Introduction to UNIX Shell Exercises Determining Your Shell Open a new window or use an existing window for this exercise. Observe your shell prompt - is it a $ or %? What does this tell you? Find out which shell you are using with the command: echo $SHELL Find out which shells are available on your system: which csh which tcsh which ksh which bh which bash Try invoking other shells and notice how the prompt changes. Then list your processes to see them sh csh ksh bash ps Return to your original shell by ing consecutively from each of the shells you started above. List your processes to verify that you have only your original (tcsh) shell running: ps 7. Change to csh for the rest of this exercise tcsh 8. Set up a history for later on set history = 150 1
Processes Review the man page for the ps command man ps 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 Review the man page for the kill command man kill 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? 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? 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. 9. 10. Bring the third sleep process into the foreground with the command fg %3 What happens? Can you run anything else now? Why? Suspend the foreground sleep process with the CTRL-Z command. What happens? Try the jobs command now. What does it show? A suspended process can be put in either the background or foreground. Use the bg %3 2
command to resume running the third sleep process in the background. 1 1 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? 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 Redirection 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. Redirect the output of the ls -l command to a file and then view the file after the command completes: ls -l ~/* > homedir.files 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 Redirection will overwrite existing files. To prove it, try the following sequence of commands: echo 'one' > file1 echo 'two' > file1 3
You can append redirected output to an existing file if you use >> instead of >. Try to commands below to prove it: echo 'one' > file1 echo 'two' >> file1 Pipes and Filters 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 View all system processes one screen at a time by piping the output of the ps aux command to the input of the more command: ps aux more View all $USER processes one screen at a time by piping the output of the ps aux command to the input of the grep command: ps aux grep $USER 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 aux grep root more Features (csh) Command History: At the start you invoked the TC Shell's command history feature. To view your command history simply type the command history. What do you see? 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 Tcsh 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. Event Reexecution: Try reexecuting several events from your history list. An example is 4
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!! Modifying Previous Events: Type history -r > unsorted. Then, 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. 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 /Applications/* ls /Applications/i* ls /Applications/[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. 5
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 Variables 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 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 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 Try using variables with UNIX commands. For example: ls -l $HOME echo My userid is $user and my machine is $HOST 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) 6
echo $colors[1] echo $colors[2] echo $colors[3] echo $colors @ num1 = 256 @ num2 = 512 @ num3 = ($num1 + $num2) echo $num3 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 Initialization Files 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 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. Use the command source.cshrc to effect the changes you made. Try them out...do they work? 7