Changes

Jump to: navigation, search

BASH Redirection

2,745 bytes added, 10:11, 21 September 2008
Standard File Descriptors
|}
When running commands from the command line, all three of these file descriptors is are connected to the terminal, so the input to process comes from the terminal (keyboard), and normal output as well as error output goes to the terminal (screen).
The meaning of these file descriptors may vary in some contexts; for example, when a program is executed as a CGI script by a webserver, the CGI standard dictates that stdin receives POSTed data from the browser, and stdout is used to send the resulting data (typically HTML) from the script. Stderr is usually sent to a logfile.
cal 16 2009 >output 2>error
This command will fail because there is no 16th month in the year 2009 (or any year!). The stdout is redirected to the file "output", but there will be no output. The stderr (error message) will be redirected to the file "calendar.txterror".
== Redirecting Input ==
[[BASH]] uses the <code><</code> and <code><<</code> symbols to redirect input.The <code><</code> symbol used after a command and arguments and is followed by the filename from which input will be taken. If the input file does not exist, an error message is displayed. For example:  mail -s "Suggestion" joe@example.com <draft3.txt This command will mail the contents of the file "draft3.txt" to joe@example.com with a subject line of "Suggestion". The <code><<</code> symbol is only used within scripts. It is placed after a command and arguments and is followed by a text label. The lines after the command up to (but not including) the line containing the text label are used as the stdin of the command. For example:  mail -s "Warning" joe@example.com <<EOF This is a warning that the disk space on your system has fallen below 1 GB. It is recommended that you delete some files to free up additional disk space. EOF This will use the three lines after the <code>mail</code> command and before the EOF line as the stdin to the <code>mail</code> command (i.e., the body of the e-mail message). == Duplicating File Descriptors == The syntax <code>''a''>&''b''</code> can be used to redirect output for file descriptor ''a'' to file descriptor ''b''. This is commonly used to redirect stdout and stderr to the same location:  cal 16 2008 >all-output 2>&1 This redirects stdout to "all-output" and then sends stderr to the same location. Note that the order of redirection matters -- if the command was written in this way the result would be different:  cal 16 2008 2>&1 >all-output (In this case, stderr is redirected to the same place as stdout, which is the terminal, and then the stdout (alone) is redirected to the file "all-output"). A shortcut syntax permits stderr and stdout to be redirected to the same destination:  &>''filename'' = Using Pipes = A pipe is a buffered mechanism for connecting the output of one command to the input of another. The symbol used is <code>|</code>:  cal | lpr This prints the current calendar on the (default) printer. Multiple pipes may be used together in a long pipeline:  grep "/bin/bash$" /etc/passwd | cut -d: -f1 | sort | mail -s "Bash users" joe@example.com This pipeline selects all BASH users from the system account file /etc/passwd, cuts out the user name, sorts them into order, and e-mails them to joe@example.com with the subject line "Bash users". You can use the <code>2>&1</code> syntax with pipes:  ls /dev/snd/* /dev/asrf2as/423asfd 2>&1 | lpr Both the stdout and stderr of the <code>ls</code> command will be sent to the printer. = Using stdout as an Argument = The <code>$( )</code> can be used to capture stdout of a command and use it as an argument to another command:  vi $(date +%Y)-notes.txt  [[Category:BASH]][[Category:Linux]]

Navigation menu