," redirects the output of a command into a file. That is, it changes standard output. An example of this would be ls /bin > myfile.standard output"> ," redirects the output of a command into a file. That is, it changes standard output. An example of this would be ls /bin > myfile.standard output">
Download presentation
Presentation is loading. Please wait.
Published byBrendan Charles Modified over 9 years ago
1
Pipes and Redirection in Linux ASFA Programming III 2011-2012 C. Yarbrough
2
Redirecting standard in (stdin) more <myfile This tells the more command to take its standard input from the file myfile instead of from the keyboard or some other file. (Remember, even when stdin is the keyboard, it is still seen as a file.) standard input
3
Redirecting standard out (stdout) The more common of the two, ">," redirects the output of a command into a file. That is, it changes standard output. An example of this would be ls /bin > myfile.standard output
4
Redirecting standard error (stderr) If we want to get the output and any error messages to go to the same place, we can do that. Using the same example with ls, the command would be: ls /bin /jimmo > /tmp/junk 2>&1 The new part of the command is 2>&1, which says that file descriptor 2 (stderr) should go to the same place as file descriptor 1 (stdout). By changing the command slightlydescriptor ls /bin /jimmo > /tmp/junk 2>/tmp/errors we can tell the shell to send any errors someplace else.shell
6
Pipes The most commonly used character is "|", which is referred to as the pipe symbol, or simply pipe.pipe This enables you to pass the output of one command through the input of another.
7
. For example, say you would like to do a long directory listing of the /bin directory. If you type ls -l and then press Enter, the names flash by much too fast for you to read. When the display finally stops, all you see is the last twenty entries or so. If instead we ran the command ls -l | more the output of the ls command will be "piped through more". In this way, we can scan through the list a screenful at a time.
8
Redirection can also be combined with pipes like this: sort < names | head or ps | grep sh > ps.save
9
In the first example, the standard input of the sort command is redirected to point to the file names. Its output is then passed to the pipe. The standard input of the head command (which takes the first ten lines) also comes from thepipe. This would be the same as the commandstandard inputpipe sort names | head which we see here: In the second example, the ps command (process status) is piped through grep and all of the output is redirected to the file ps.save. If we want to redirect stderr, we can. The syntax is similar:stderr command 2> file
10
It's possible to input multiple commands on the same command line. This can be accomplished by using a semi-colon (;) between commands. I have used this on occasion to create command lines like this:command line man bash | col -b > man.tmp; vi man.tmp; rm man.tmp man bash | col -b > man.tmp; vi man.tmp; rm man.tmp
11
This command redirects the output of the man- page for bash into the file man.tmp. (The pipe through col -b is necessary because of the way the man-pages are formatted.) Next, we are brought into the vi editor with the file man.tmp. After I exit vi, the command continues and removes my temporary file man.tmp. (After about the third time of doing this, it got pretty monotonous, so I created a shell script to do this for me. I'll talk more about shell scripts later.)man- pagepipe
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.