Pipes and Redirection in Linux ASFA Programming III 2011-2012 C. Yarbrough.

11
Pipes and Redirection in Linux ASFA Programming III 2011-2012 C. Yarbrough

Transcript of Pipes and Redirection in Linux ASFA Programming III 2011-2012 C. Yarbrough.

Page 1: Pipes and Redirection in Linux ASFA Programming III 2011-2012 C. Yarbrough.

Pipes and Redirection in Linux

ASFA Programming III

2011-2012 C. Yarbrough

Page 2: Pipes and Redirection in Linux ASFA Programming III 2011-2012 C. Yarbrough.

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.)

Page 3: Pipes and Redirection in Linux ASFA Programming III 2011-2012 C. Yarbrough.

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.

Page 4: Pipes and Redirection in Linux ASFA Programming III 2011-2012 C. Yarbrough.

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 slightly

• ls /bin /jimmo > /tmp/junk 2>/tmp/errors we can tell the shell to send any errors someplace else.

Page 5: Pipes and Redirection in Linux ASFA Programming III 2011-2012 C. Yarbrough.
Page 6: Pipes and Redirection in Linux ASFA Programming III 2011-2012 C. Yarbrough.

Pipes

• The most commonly used character is "|", which is referred to as the pipe symbol, or simply pipe.

• This enables you to pass the output of one command through the input of another.

Page 7: Pipes and Redirection in Linux ASFA Programming III 2011-2012 C. Yarbrough.

• . 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.

Page 9: Pipes and Redirection in Linux ASFA Programming III 2011-2012 C. Yarbrough.

• 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 command

• 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:• command 2> file

Page 11: Pipes and Redirection in Linux ASFA Programming III 2011-2012 C. Yarbrough.

• 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.)