NEW BRUNSWICK COMPUTING SERVICES
USER SERVICES GROUP

Intermediate Use Of The UNIX Operating System
Exercise Answers

Answers to practice exercises

Practice 1 - Customizing your environment

After completing this lab, you will be able to distinguish among three basic UNIX shells, to use the search path, to set and use variables, to create aliases, and to create shell scripts.

  1. In the login session, enter the command: cd ; cd UNXclass ; echo $0
    What was the response? -tcsh
    Which shell are you in? T Shell

  2. Enter the command: ./newshell.1
    What shell is "newshell.1"? (Use the technique in problem #1) C shell
    Exit this shell and then enter the command: ./newshell.2
    What shell is "newshell.2"? K shell
    Exit this shell and then enter the command: ./newshell.3
    What shell is "newshell.3"? Bash
    Make sure you exit from this shell back to your login shell.

  3. Use the "echo" command to display the value of your "EDITOR" variable.
    Write the command here: echo $EDITOR
    Put the results here: Your mileage may vary, probably something like /usr/local/bin/emacs

  4. Start a new session on Eden or RCI, DO NOT logout of your original session.
    Once logged in type the command setenv PATH . and then try the commands ls, who, or ps, What happened?
    Nothing worked, since the shell cannot find any commands not in the current directory.

  5. In the session with the faulty PATH setting, how can you use how can you use "emacs" if it is not in your path (hint see #3)?
    Use the full path; i.e., /usr/local/bin/emacs or use $EDITOR to invoke emacs

  6. EXTRA: On Eden, type the command:
    mv ~/.cshrc ~/.cshrc.orig ; cp ~/.cshrc.orig ~/.cshrc
    (The above is to preserve the current working settings in case the editing you are about to do messes up your startup file). Create an alias called "pf" to run "ps -ef". Create it at the command line, and when you have got it working use emacs to add it to the bottom of your current .cshrc (on RCI you may have to create a new .cshrc file). Make sure to end the line you add with a carriage return and to save and exit the emacs session.
    What is the command that creates the alias? In the T shell or C shell alias pf "ps -ef"

    Start a new session on RCI or Eden. Then try the pf command.

  7. EXTRA: Create a shell script to print out information about your own personal login environment. Name it "myinfo". It should use the commands "ps", "id", "whoami", and "pwd". Write the script here (it should be short!)

    #!/bin/tcsh
    id
    ps
    whoami
    pwd


End of Practice 1 - Customizing your Environment


Practice 2 - Working with Processes

This lab demonstrates the use of ps to list processes, and kill to end them.

  1. Execute the ps command with no options. This shows the processes running in the current login session. List your processes below.
    (your mileage will vary)
    Re-enter any extra processes you may be running (except tcsh) and end them before proceeding.

  2. Run the following command; once the ls starts, use <CTRL>c to terminate it:

    cd ~ ; ls -lR ..

  3. Run the ls process again, but this time put it in the background with &:

    ls -lR .. &

    Since you put it in the background you might think that you would get your prompt back and be able to type in more commands at the terminal. What actually happened? Even though you do get your prompt back, you can't type because the output of ls is still going to the standard output (the screen).

    Type a % followed by a carriage return to bring the above to the foreground then a <CTRL>C to terminate it.

  4. Use the cd command to go to the UNXclass subdirectory.

  5. Type the command:

    emacs big.file &

    then run "ps" to get the PID of the emacs process. Use the PID for the emacs session to kill it.
    What command did you use? kill PID Where PID is the process ID of the "emacs" session.
    If the emacs session's job number was 1, what other command would have killed it? kill %1

End of Practice 2 - Working with Processes


Practice 3 - Input/Output Redirection

This lab provides practice in using output redirection and pipes.

  1. Do a command similar to the "ls" command from practice 2, problem 4, but this time redirect the standard output to /dev/null (so we don't fill up the hard drive) so that you really do get your command line back:

    ls -lR ../.. >& /dev/null &

    Having typed this in, quickly run "ps". What is the PID (Process ID) of the ls process? your process ID here....
    Note: If the "ls" process is no longer running, repeat step 1 with "; ps" added to the end of the command.
  2. Execute the following command:

    ls > output.1

    What are the contents of "output.1"? The output of ls, because we redirected its standard output to that file.

  3. Execute the following commands:

    ls > output.2
    who > output.2
    ps > output.2


    Use more to check the contents of "output.2".
    What is there? The output of ps.
    Why? > erases the output file each time it is used

  4. Now execute the following commands:

    ls > output.3
    who >> output.3
    ps >> output.3


    What is the contents of output.3? The output of all three commands.
    Why is this different from output.2? >> appends to the output file instead of erasing it.

  5. Create a pipeline one step at a time, you will add one command to the pipeline in each step, and notice how the output changes. Begin with a single command, "ps", with the options for a full listing of every process:

    ps -fe

  6. Use command line editing to add more, to display the output one screenful at a time. You are instructing the shell to take the standard output of ps and feed it into the standard input of more:

    ps -fe | more

  7. Add grep root to filter for the string root. The standard output of ps goes into the standard input of grep, The standard output of grep goes into the standard input of more:

    ps -fe | grep root | more

  8. Add sort so the output gets sorted:

    ps -fe | grep root | sort | more

  9. Add pr so the output gets prepped for printing:

    ps -fe | grep root | sort | pr -h "Root Processes" | more

    Now you have connected the input and output of five processes. The net result is that you get a sorted list of processes containing the string root, with a header, and pausing after every screenful.

  10. Are all of the above processes owned (run) by root? No.
    Why? Any process that has the string "root" in the listing will be displayed including any running "grep root".

    Use "man ps" to find a "ps" option that limits the display to one user. Using this how would you re-write the above pipeline to display ONLY processes owned by root? ps -fu root | sort | pr -h "Root Processes" | more

  11. Optional: Restore the original startup settings to your account.
    On RCI:
    mv .login .login.class ; mv .login.OLD .login
    mv .cshrc .cshrc.class ; mv .cshrc.OLD .cshrc


    On Eden:
    mv .login.local .login.local.class
    mv .login.local.OLD .login.local
    mv .cshrc .cshrc.class ; mv .cshrc.OLD .cshrc


End of Practice 3 - Input/Output Redirection

Answers to practice exercises

7/26/04

Rutgers University Computing Services

UNX 3 ES