Difference between revisions of "BASH Redirection"

From CDOT Wiki
Jump to: navigation, search
(New page: = Standard File Descriptors = Linux systems use ''file descriptors'' to keep track of open files. Normally, three file descriptors are automatically opened for a process (running instance...)
 
(Redirecting Input)
Line 61: Line 61:
 
== Redirecting Input ==
 
== Redirecting Input ==
  
[[BASH]] uses the <code><</code> and <code><<</code> symbols to redirect 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).
 +
 
 +
== Cloning File Descriptors ==

Revision as of 21:12, 15 September 2008

Standard File Descriptors

Linux systems use file descriptors to keep track of open files. Normally, three file descriptors are automatically opened for a process (running instance of a program):

File Descriptor Number Name Short Name Purpose
0 Standard Input stdin The normal input to the process.
1 Standard Output stdout The normal output from the process.
2 Standard Error stderr Error message output from the process.

When running commands from the command line, all three of these file descriptors is 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.

Additional file descriptors may be opened by a process.

Redirecting the Standard File Descriptors

BASH (and other shells in the Bourne family) permit input and output to be redirected, so that it does not come from or go to the terminal.

Redirecting Output

In BASH it is possible to redirect output using the > and >> symbols after a command and arguments, followed by the filename which will contain the redirected output. These symbols may be preceeded by a file descriptor number; if omitted, stdout is assumed. The > symbol will truncate the file if it already exists; >> will append to the existing file. Both symbols will create the file if it does not exist.

Some examples:

cal >calendar.txt

Runs the cal command (which outputs a calendar of the current month by default) and places the output in the file "calendar.txt". If the file "calendar.txt" already exists, it is truncated (the existing contents are wiped out). If it does not exist, it is created.

cal 3 2025 >>calendar.txt

Appends to the calendar for March, 2008 to the file "calendar.txt". The file is created if it does not exist.

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.txt".

Redirecting Input

BASH uses the < and << symbols to redirect input. The < 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 << 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 mail command and before the EOF line as the stdin to the mail command (i.e., the body of the e-mail message).

Cloning File Descriptors