Introduction to UNIX
Shell Exercises


Determining Your Shell

  1. Open a new window or use an existing window for this exercise. Then make sure that you are in the Shell subdirectory:
    
         cd ~/Shell
         

  2. Observe your shell prompt - is it a $ or %? What does this tell you?

  3. Find out which shell you are using with the command:
    
         echo $SHELL
         

  4. Find out which shells are available on your system:
    
         which csh
         which tcsh
         which ksh
         which bh
         which bash
         

  5. Try invoking other shells and notice how the prompt changes. Then list your processes to see them
    
         sh
         csh
         ksh
         bash
         ps
         

  6. Return to your original shell by exiting consecutively from each of the shells you started above. List your processes to verify that you have only your original (tcsh) shell running:
    
         exit
         exit
         exit
         exit
         ps
         
  7. Return to the tutorial to learn about the next section before proceeding.

Processes

  1. Review the man page for the ps command
    
         man ps
         

  2. Try using the ps command to list your processes. Then try to list other processes on the system. For example:
    
         ps
         ps uc
         ps ug
         ps aux
         

  3. Review the man page for the kill command
    
         man kill
         

  4. Start a process which does nothing except sleep for 5000 seconds
    
         sleep 5000
         
    Now try issuing some other commands, for example ls. What happens? Why won't the shell accept any commands?

  5. To kill a process which is running in the foreground, CTRL-C can usually be used. Kill the sleep 5000 process you just started in this manner. What happens after you kill the process? Try another UNIX command, such as ls. Does it work now?

  6. Start another sleep 5000 process, except this time, start it in the background:
    
         sleep 5000 &
         
    Now try issuing some other commands, for example ls. What happens?

  7. Use the jobs command to find out about jobs you have running in the background. What does the output of the jobs command tell you?

    Start a couple more sleep 5000 processes in the background and then use the jobs command again. Do they all appear?

  8. Bring the third sleep process into the foreground with the command fg %3 What happens? Can you run anything else now? Why?

  9. Suspend the foreground sleep process with the CTRL-Z command. What happens? Try the jobs command now. What does it show?

  10. A suspended process can be put in either the background or foreground. Use the bg %3 command to resume running the third sleep process in the background.

  11. The kill command can be used in various ways. Kill the third sleep process with the command kill %3 and then use the jobs command to check on it. What do you see?

  12. Now use the ps command to view your processes. Do you see the remaining two sleep processes? Notice their PIDs and then use the kill command to kill both of them. For example:
         % ps
            PID    TTY  TIME CMD
          15493  pts/0  0:00 sleep 1000 
          18054  pts/0  0:00 sleep 1000 
          18309  pts/0  0:00 -tcsh 
          27017  pts/0  0:00 ps 
         % kill 15493 18054
         % ps
            PID    TTY  TIME CMD
          15498  pts/0  0:00 ps 
          18309  pts/0  0:00 -tcsh 
         [3]  + Terminated             sleep 1000
         [2]  + Terminated             sleep 1000
         
  13. Return to the tutorial to learn about the next section before proceeding.

Redirection

  1. Redirect the output of the man command into a file instead of to your screen with the command:
    
         man cp > cp.manpage
         
    List your directory to confirm that the command worked. View the file with the more cp.manpage command also.

  2. Redirect the output of the ls -l command to a file and then view the file after the command completes:
    
         ls -l ~/*  > homedir.files
         

  3. Redirect the input for mail by sending a file as the mail message. No interactive prompting will be done by mail. Don't forget to substitute your userid and node number for the Xs below.
    
         mail studntXX@nodeX.class.mhpcc.edu  <  homedir.files
         

  4. Redirect both the stdout and stderr from a command to a file. For example, type the command ls * nosuchfile and notice that both the command output and an error message are displayed on your screen. Then, type the command below to redirect both to a file. View the file after the command completes.
    
         ls * nosuchfile >&  ls.out-err
         

  5. Redirection will overwrite existing files. To prove it, try the following sequence of commands:
    
         echo 'one' > file1
         cat file1
         echo 'two' > file1
         cat file1
         

  6. You can append redirected output to an existing file if you use >> instead of >. Try to commands below to prove it:
    
         echo 'one' > file1
         cat file1
         echo 'two' >> file1
         cat file1
         

  7. Redirection of both stdin and stdout can also be used. Assuming that you are still in your ~/Shell directory, the following command will use the file unsorted as input to the sort utility and produce a sorted file called sorted.list.
    
         sort < unsorted  > sorted.list 
         

  8. Return to the tutorial to learn about the next section before proceeding.

Pipes and Filters

  1. Obtain a sorted list of users on the system by piping the output of the who command to the input of the sort command:
    
         who  |  sort
         

  2. View all system processes one screen at a time by piping the output of the ps ug command to the input of the more command:
    
         ps ug  |  more
         

  3. View all studntXX processes one screen at a time by piping the output of the ps ug command to the input of the grep command:
    
         ps ug  |  grep studnt
         

  4. View all root processes one screen at a time by piping the output of three different commands together. Note that in this example, the grep command is acting as a filter:
    
         ps ug  |  grep root | more
         

  5. Return to the tutorial to learn about the next section before proceeding.

Features (csh)

  1. Command History: The student userids have already been setup to use the C Shell's command history feature. To view your command history simply type the command history. What do you see?

  2. You may wish to view your command history in reverse chronological order (most recent commands at beginning of list), one screen at a time:
    
         history -r | more
         

  3. The student userids have been setup to use the Tcsh, which is an extended version of the C Shell. It allows you to "cycle through" your command history by using the up arrow key. Try pressing your up arrow key at the Shell prompt. Try it several times. Your previous commands should appear, allowing you to edit them and/or press return to reexecute them.

  4. Event Reexecution: Try reexecuting several events from your history list. An example is shown below - note that your actual command history will differ:
         % history -r more
           36  12:34   cat sorted.list
           35  12:34   sort < unsorted > sorted.list
           34  12:34   ls -l > unsorted
           33  12:32   sort < ls * > sorted.list
           32  12:23   ps ug | grep studnt
           31  12:22   ps ug | grep root | more
           30  12:20   ps ug | more
           29  12:18   ps aux
           28  12:16   ls
         % !36
         
    Now try reexecuting an event by using the !string method. For example, the command !his will reexecute the last command you issued which began with the string "his" (such as a history command).

    Finally, try reexcuting a previous command by typing !!

  5. Modifying Previous Events: First, type a command with an obvious typo:
    
         sort  unsrted | grep Systems
         
    After the shell tells you it can't open the file "unsrted", modify the command as shown below. The command should now work.
    
         ^srt^sort
         
    Using the Tcsh, you could also accomplish this by recalling the command with the up arrow key and then editing the command line.

  6. Using Aliases: Create a few aliases and then use them:
    
         alias  h     "history -r | more"
         h
         alias ll     "ls -l"
         ll
         alias rm     "rm -i"
         rm unsorted
         
    Issue the alias command without any arguments. It should show you all of your defined aliases.

  7. Filename Generation: You have been using this feature all along. For example:
    
         ls ~/Filesystem/*
         ls ~/Filesystem/file*
         ls ~/Filesystem/[ns]*
         
    You can turn off filename generation with the command set noglob. Do this and then try the same commands above. When you're convinced that it is turned off, turn it back on again with the command unset noglob.

  8. Filename Completion: Turn on this feature with the command set filec. Then, create a file with a long name:
    
         touch Introduction.UNIX.Filesystems
         
    Now, type the following and notice what happens. Note that you should use the actual Tab key instead of the literal words "Tab key". Also note that some systems use the Esc key instead of the Tab key.
    
         ls IntTab key
         

  9. Return to the tutorial to learn about the next section before proceeding.

Variables

  1. Display your shell's variables with the set command. Review the list. Then, use the echo command to display a few of them individually. For example:
    
         echo $user
         echo $home
         echo $term
         

  2. The PATH(path) variable is a very important variable. It determines the directory paths which the shell will search when looking for an executable. Find out what your path is:
    
         echo $path
         echo $path | fold   - use this if it "runs off" the 
                               right hand side of the screen 
         

  3. Display your shell's environment variables with the setenv command. Review the list. Then, use the echo command to display a few of them individually. For example:
    
         echo $HOST
         echo $MANPATH
         echo $HOME
         

  4. Try using variables with UNIX commands. For example:
    
         ls -l $HOME
         echo My userid is $user and my machine is $HOST
         

  5. Set a few variables of your own creation and then display/use them. For example:
    
         set myvar1 = "this is a string variable"
         echo $myvar1
    
         setenv dir1 /usr/local/bin
         ls $dir1
    
         set colors=(red green blue) 
         echo $colors[1]
         echo $colors[2]
         echo $colors[3]
         echo $colors
    
         @ num1 = 256
         @ num2 = 512
         @ num3 = ($num1 + $num2)
         echo $num3
         

  6. Unset any of the variables you previously set and then try to display them. What happens? For example:
    
         unset colors
         echo $colors
    
         unsetenv dir1
         echo $dir1
         

  7. Return to the tutorial to learn about the next section before proceeding.

Initialization Files

  1. Change to your home directory and list all of your files including your hidden files. Which initialization files mentioned in the tutorial do you see?
    
         cd
         ls -a
         

  2. Use either vi or pico to edit your .cshrc file. Notice the variables and aliases which have already been set for you. Customize the file, perhaps creating some variables or aliases of your own. You may get an idea or two from the example .cshrc file in the tutorial.

  3. Use the command source .cshrc to effect the changes you made. Try them out...do they work?

  4. View the system wide initialization file: /etc/environment. Note in particular how the PATH variable is set. This is the default path for all users on the system.

  5. This concludes the Shell exercises. Return to the Shell tutorial or to the Table of Contents.