ps -ef | sort | more
The ps command will output a list of all processes to stdout. Normally, this would be to the console window. The pipe (|) will tell UNIX to take the output of ps and make it the input to sort. Then the output from sort will become the input to more.
Without using pipes I could do:
ps -ef > temp_file1 sort < temp_file1 > temp_file2 rm temp_file1 more temp_file2 rm temp_file2
This is like using the pipe but instead we put the output of ps into temp_file1. Then we use temp_file1 as the input to sort and send the output to temp_file2. Finally, we use temp_file2 as the input to more. You should be able to see how this is a lot like the first example using pipes.
Now here is a third way using Named Pipes. To create a named pipe use:
mkfifo temp_file1
If you list this entry using ls -l you will see something like:
prw-r--r-- 1 dgrainge staff 0 Mar 23 08:13 stdout
Notice the first letter is not - for a file or even d for a directory. It is p for a named pipe. Also the size of the 'file' is 0. We will need two shells to do this.
# shell 1 mkfifo temp_pipe1 mkfifo temp_pipe2 ps -ef > temp_pipe1 # this will block so switch to shell 2 # shell 2 sort < temp_pipe1 > temp_pipe2 # this will block so switch back to shell 1 # shell 1 more temp_pipe2 rm temp_pipe1 rm temp_pipe2
The interesting thing about this example is that we needed two shells to do this. At first this might seem like a downside but the truth is, this is a positive. I can do something like:
mkfifo stdout mkfifo stderr # shell 2 more stdout # shell 3 more stderr # shell 1 sh -x some_script.sh 1> stdout 2> stderr
The -x will turn on trace. Debug information will be output to stderr. By using the named pipes, I can redirect the regular output to shell 2 and the debug information to shell 3.
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.