CSE 271 Lab 2: Using UNIX

Announcements

Remember Homework #2 due Friday and Program #1 due Sunday. Recall also that everything in UNIX is, by default, case sensitive. If you create a file called "done" in your directory for a homework, it will not be found when we are looking for "DONE".

During the following, don't forget how to learn about the commands described. Normally we will not explain all aspects of a command, and in some cases, enough information is not provided here to know how to do something.

Open a shell (terminal window) in your desktop environment

When you are not in the lab, use an ssh client to connect to sunlab.cse.lehigh.edu, and log in with your username and password. When complete, you will be logged in, and the sun will be running a bash shell for you, waiting for your command.

Optional arguments to ls

In class we used ls -l to list files in a long format (size, permissions, etc.). What option to ls will show files that start with a period?

Finding programs to run

Sometimes there is more than one version of a program installed. On our Suns, du is one example. (Others include df, ls, and many more.)

The whereis command (installed on the suns in /usr/ucb/whereis) will look for programs in a bunch of standard places. Use whereis to find the two locations for du (which is a program that tells you how much disk storage is being used). (The du.1 and du.1b files are man page entries.)

But which one is being run when you type du? Use the which command to find out. Different versions of the same program will not always produce the same output (which is certainly the case for du). Fortunately, you can always specify the complete (or relative) path of a program to override the default binary.

Now use whereis to find out where gcc is installed. Compare that result to the location returned by which for gcc. Something is fishy, right?

So how does UNIX decide what programs you can run, just by typing their name? It uses your path, which is an environment variable (i.e., something that you can change) called PATH. Type env to see all your environment variables. Your shell will look in each directory in the order specified in your path to see if there is a program with the name you entered. One of those directories should match the directory in which gcc is found (which you saw earlier with which).

Wildcards and Escapes

Instead of explicitly specifying a filename or directory as a parameter to a program, you can use wildcards (e.g., instead of tab completion). [In the examples that follow, the text in bold represents text that a user typed; the remainder is either my shell prompt or the output of the program.] Note that in the next examples, ls is showing the contents of the directory (or directories) passed as paramters to ls. In reality, the shell expanded the wildcard to instead be the list of filenames that match, so that the program that is called never sees the wildcards---it just sees the parameters as if the user had typed them. Arguments to commands are separated by whitespace, so spaces and other special characters must be escaped or quoted, as in: touch is a program that will create a new (empty) file if one does not exist, and will change the last-modification-time of an existing file.

Copy and Move UNIX Files

cp and mv work more or less similarly. You give them a list of source files and a destination (except that mv removes the old file). mv is also used to rename files.

Examples:

Creating and Removing Directories

mkdir and rmdir are the usual programs, but sometimes using rm is easier.

Text-based Web browsing

Sometimes it is helpful to be able to browse the web from the machine to which you are connected, rather than the one you are sitting in front of. For example, some websites are restricted to users within some domain (such as .lehigh.edu). Fortunately, most UNIX installations have one or more text-based web browsers installed. The most common is called lynx. Use lynx to visit your favorite web page and see whether the page is still useful (since it won't show graphics, javascript or flash, etc.).

Finish mycat.c

On Monday we started to write our own version of the cat utility. Let's complete it today. At the end of this process we should have a program that compiles without errors and without warnings.

Now we can add some functionality. Recall that "cat -n" added line numbers to the output. Change your implementation of mycat.c so that it adds line numbers similarly (including similar spacing). You might need to remember a bit of how printf() works, and of course figure out when to print the line number. If you have time, change your version to accept one or more arguments, and open and close each file in turn (just as regular cat does).


Last revised: 21 January 2013, Prof. Davison.