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.]io:~/cse271.131% ls
Note that in the next examples, ls is showing the contents of the directory (or directories) passed as paramters to ls.
hw1/io:~/cse271.131% 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.
DONEio:~/cse271.131% ls ?w?
Arguments to commands are separated by whitespace, so spaces and other special characters must be escaped or quoted, as in:
DONE
io:~/cse271.131%io:~% touch "this is a test"
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.
io:~% touch this\ is\ a\ test
io:~% ls th*
this is a testCopy 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:
Copy mozilla180.jpg to the images directory
% cp mozilla180.jpg images/
Copy this_dir and all its subdirectories to /tmp
% cp -r this_dir/ /tmp
Move Makefile and all .c and .h files to the cse213 directory
% mv Makefile *.c *.h cse213/
Move a file from /tmp to the current directory
% mv /tmp/d4x-2.02-2.i386.rpm .
Rename a file
% mv foo.c rename.cCreating and Removing Directories
mkdir and rmdir are the usual programs, but sometimes using rm is easier.io:~% mkdir test1
io:~% mkdir test2
io:~% touch test1/hello
io:~% rmdir test1
rmdir: directory "test1": Directory not empty
io:~% rmdir test2
io:~% rm -rf test1
io:~%
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.
- First, create your own directory in which to work and cd into it to make it your current working directory.
- Now let's copy the version we ended with on Monday (plus some comments which I added), which you can find here. How shall we get it into your directory? Well, there are a few good ways (besides copy and paste). Let's retrieve a copy directly. We could use lynx, as described above, to get it. But instead, let's use wget
- wget is a utility to get files from the web. It has a man page, but it is rather long. Since wget is a program from the gnu project, this is an opportunity to try out the info reader. To learn how to use info, type "info info" and follow the instructions. When you are comfortable with info, type "info wget" to learn how to use wget. Once you are familiar with wget, use it to retrieve a copy of mycat.c. Note that you'll want to rename the downloaded file to mycat.c.
- Now we can work with mycat.c. First, create a Makefile that will compile mycat.c into the executable mycat. (For those of you still thinking in C++, make sure you use gcc, not g++.) Type make to execute the Makefile rules and be certain that your Makefile is correct---it should generate no errors.
- We should practice defensive programming so modify your Makefile to use the -Wall option when compiling. This tells the compiler to generate a warning message whenever it notices any potential problems. Since you probably didn't include the Makefile as a dependency for compilation, you need to delete your executable and any .o files (perhaps using make clean), and then run make again.
- Notice how the compiler now complains about exit(). Fix your program and run make to re-build it.
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).