CSE 271 Lab 3: Using UNIX part 2

Announcements

Recursive dice

In class Monday we implemented a simple program to partially simulate the repeated throwing of one die. Make a copy of the version we created in class. We noted that we need to make a change to use srand() so that rand() does not always give the same sequence of numbers. Note that since random number generators produce a sequence of random numbers, you only need to set the seed at the beginning (which we did) to ensure that the sequence will (likely) be different.

Once you have a version that feels truly random, modify it so that you have a function that rolls two dice, prints their values, and calculates the sum. Repeatedly call that function until the sum is some arbitrary desired value.

Make a copy of your working program to a new file (rdice.c). Modify the dice rolling function to be recursive, so that main() needs only call it once and it will call itself repeatedly until the desired sum is achieved. Give yourself some imaginary bonus points if you implement the function using tail recursion as that is usually most efficient. (Tail recursion is when the last step of the function is to call itself again.)

ssh, scp, sftp

As you know, ssh provides a secure mechanism to connect from one machine to another and start a shell. ssh also provides the foundation for a few other commands, such as scp and sftp, which you can think of as secure copy and secure ftp (file transfer protocol). The suns have each of these installed. You can run ssh at a shell to connect to another sun if you like, e.g.:

So, the above demonstrates how I used ssh to connect from tethys to umbria and get a shell running there. Since I had not done so previously, ssh warned me that the secure fingerprint provided by umbria was not one that I had seen (and accepted before), and asks me to continue. It won't ask me that again (unless umbria's fingerprint were to change for some reason).

If I am running X-Window on my current machine and wanted to be able to run programs that open new windows from the new machine, I would use ssh -X instead of just ssh. Since the window display information is flowing through your secure ssh connection, all X11 commands are sent back to the source machine encrypted, just as your shell commands are.

The scp and sftp programs allow you to securely copy one (scp) or more (sftp) files from one machine to another. Since I have two user accounts, I could use scp to copy files from one to another, such as:

We can use sftp to allow us to connect and use ftp-like commands to get and put files to a remote host, such as some of the university machines to which you have access.

So here I used sftp to copy a file from my current account (dot-emacs) to my university afs space by connecting to part of the HPC cluster of systems running linux in the LTS server room. (You can find out about more such systems here.)

Changing your password

You were given an initial password to access your CSE account. For increased security, it is often a good idea to periodically change it. Your university account requires you to change it every six months, but the CSE accounts are not so strict. When you do want to change it, you'll use the passwd command, as in:

Note that it requires that you know your old password first, and will prevent you from re-using passwords that are too similar. The best passwords are often those that are abbreviations for something that is absurdly funny or offensive (to make it easy to remember).

Disk usage and quota

As you continue to use your CSE account for multiple classes and perhaps email and web pages, too, you will be using more and more storage. A new user's storage allocation is small, but you can request additional storage easily if needed (email to help@cse...).

There are two UNIX commands that can help you understand how much disk space you are using. The first is du, which stands for disk usage. By default it tells you how many blocks of disk you are using in the current directory and within any subdirectories. A block in Solaris is typically 512 bytes, but the -h option will convert it to something more useful. Try it out on your account and see how much storage you are currently using.

The second command is quota -v. It often shows how much total storage you have used on the filesystem and what your quota is. Back when we used quotas, it showed something like this:

So from this I could see that I was using 20MB in my home directory and my limit is 50 megabytes. The hard limit of 75MB allows me to temporarily (for a few days) use more than the 50MB if needed. If I try to create files that sum to more than 75MB, they will fail. You can try to see what your quota is, but most likely it will not show anything (since quotas are currently not being used).

Instead, with our current ZFS filesystem, every user has their own filesystem -- almost like having your own disk. The df -h . command will tell you how much disk space is on "your" disk, how much is in use, and how much is free. (A quick aside: if you ever fill up your home directory so that there are no bytes free, then you will be in trouble -- you might not be able to log in, and even if you can log in, you won't be able to use rm to remove a file and make room. Instead you'll need to use the "magic" command: cat /dev/null > bigfiletodelete. You already know what cat does; the /dev/null file is a special file that contains nothing. So this overwrites the file of your choice with nothing, which effectively frees the space that the file used to contain.) You can then use rm to delete this file that contains nothing.

The grep command

Sometimes it is useful to find the lines of a file that match some pattern. A very simple use is to find the lines in a program that call a function. grep is a great command to use for this. For example, I can find every line that contains the string "print" in mycat.c:

I could equivalently have typed grep print < mycat.c. When used this way, we can think of grep as a filter. It is reading from standard input and filtering the content in some way and sending the remaining text to standard output.

Writing a filter

For the last task of this lab, let's write our own very simple filter. This program will be somewhat similar to our mycat program. Your task is to write a simple character filter to collapse extra white space in text files. The filter should change each tab character ('\t') to a space and collapse a series of two or more spaces into a single space. As a Unix filter, it should read its input from stdin and write its output to stdout. Do not use character arrays or strings for this program.

As an example usage using input redirection, assume the contents of the sample input file are as shown.

and once you have successfully written your collapse.c program, you can compile and run it as:

Last revised: 30 January 2013, Prof. Davison.