NEW BRUNSWICK COMPUTING SERVICES
USER SERVICES GROUP

Intermediate Use Of The UNIX Operating System
Exercise 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 disk) 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?
    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"?

  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?
    Why?

  4. Now execute the following commands:

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


    What is the contents of output.3?
    Why is this different from output.2?

  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? Why?

    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?

  11. Optional: What commands should restore the original startup settings to your account.

End of Practice 3 - Input/Output Redirection

After attempting the above look here for answers