CS 50 Software Design and Implementation


Shell Commands Lab

We have discussed a number of Linux shell commands in class. It’s time to get familiar with them and your Linux environment.

Here is what you should do: First, login to your Linux account and go through the material in Lecture 2 (The Linux Shell and Commands) and Lecture 3 (The Linux Shell and Command - Continued)(Lecture 3 notes will be posted just before the lecture) by executing the example shell commands found in the notes. You will be become familiar with the most common set of commands. Knowledge of these commands will serve you well in the course and project, and will serve as a basis for the shell programming assignment (Lab 2)

Note: Lab1 is not graded nor do you have to hand anything in.

The TA and I will be in the Sudikoff 001 during X-hour should you have any questions. There is also office hours. You should already have logged on to galehead.cs.dartmouth.edu to change your shell to bash (see notes) if need be and reset your password if Wayne provided you with a new account.

Tip: I would recommend that you complete this lab-sheet before you start the Lab 2. As I mentioned Lab 2 will comprise of a set of (progressively more difficult) shell programming problems, so you should get this lab-sheet out of the way as soon as can. If you need help on the meaning or syntax of any command you can use the manual (man) pages or the web unix commands.

Complete the following questions.


1) Go through all command examples in Lecture 2 and Lecture 3 (when available).

2) You should have already created a subdirectory for cs50 in your home directory. Change into the cs50 directory and create two new subdirectories for labs and project. Change into the labs directory and create two subdirectory lab1 and lab2 (that is where you can put your shell programs for the next assignment).

3) The file bad.spelling contains a number of spelling errors. Using the Firefox browser under Linux, copy this file to your lab1 sub-directory and use the spell command to find the spelling errors. Use emacs, vi or vim (or any preferred text editor) to correct the errors and then check the file again. Be warned that the spell command will incorrectly report some errors (as the basic dictionary file, /usr/share/dict/linux.words, is not complete; you should cat, more, or less the file to take a look at it).

4) How many words are there in the file bad.spelling Use the Linux manuals to find out how to determine this count.

5) List the processes running on your terminal window using the ps command. After examining the manual entry for ps can you determine how many processes are running simultaneously on the Linux machine you are using? How many of these can be considered “system-level” processes?” There is a lot going on under to bonnet - if you get my drift.

6) Experiment with the grep command which finds strings in text files. For example, how many lines is the word UNIX found on in the file bad.spelling Can you use grep to see how many of your processes are running? If you use ps -el you will see all the processes. You can find your processes in this list if you know your UID (user ID). How do you find your UID? (Hint: echo $something). Once you have your UID you can pipe to grep and only display your process status information in long format (“-l switch”). The resulting pipe command has the same output format as “ps -U username” (find out what the -U switch means). The output of the two command lines may differ in some cases - why? The command top shows all processes (and other information about the system). Check it out.

7) Experiment with the chmod command. Change the read, write and execute modes (one at a time) on one of your subdirectories and see how this affects the ls command, your ability to create files in the directory and your ability to change into the directory. Use both the absolute and symbolic methods (as discussed in Lecture 2) in addressing the above questions.

8) You can read the /etc/passwd file - sounds crazy. Use a combination of cat (or more) and grep to display information in /etc/passwd about your own details and others in addressing the following questions: a) use cat and grep in a pipe line to only print your information. What does this line mean? Checkout the /etc/passwd file format; b) Which character is used to separated fields in the file? c) How is your password stored? If you try and access /etc/shadow what happens? d) How many different shells can you find when looking at /etc/passwd file - which are they?

9) Setup your .bash_profile file. Set your prompt (PS1) to something you like. Set up your .bashrc file and add in the important aliases (e.g., mygcc, rm). You can cut and paste from the lecture notes or cp over the files from ~campbell/ and edit them as you wish. Use the source command to execute your new .bash_profile when you have finished editing it.

10) Difficult: The ls command seems to have every option you’d ever need. However, older versions of ls did not provide a facility to list files sorted by their size (on today’s Linux machines we can use ls -lrS ). Can you write a shell pipeline to provide this listing? We have talked about the commands you’ll need to do this. You can look at the manual to find which switches help to solve this problem.


OK. You are done.

Tip: Make sure you always logout when you are done and see the prompt to login again before you leave the terminal.