Skip to main content

Standard Input and Output Redirection

Standard Input and Output Redirection

The shell and many UNIX commands take their input from standard input (stdin), write output to standard output (stdout), and write error output to standard error (stderr). By default, standard input is connected to the terminal keyboard and standard output and error to the terminal screen.

The way of indicating an end-of-file on the default standard input, a terminal, is usually .

Redirection of I/O, for example to a file, is accomplished by specifying the destination on the command line using a redirection metacharacter followed by the desired destination.

C Shell Family

Some of the forms of redirection for the C shell family are:

Character

Action

>

Redirect standard output

>&

Redirect standard output and standard error

<

Redirect standard input

>!

Redirect standard output; overwrite file if it exists

>&!

Redirect standard output and standard error; overwrite file if it exists

|

Redirect standard output to another command (pipe)

>>

Append standard output

>>&

Append standard output and standard error


The form of a command with standard input and output redirection is:

% command -[options] [arguments] < input file > output file

If you are using csh and do not have the noclobber variable set, using > and >& to redirect output will overwrite any existing file of that name. Setting noclobber prevents this. Using >! and >&! always forces the file to be overwritten. Use >> and >>& to append output to existing files.

Redirection may fail under some circumstances: 1) if you have the variable noclobber set and you attempt to redirect output to an existing file without forcing an overwrite, 2) if you redirect output to a file you don't have write access to, and 3) if you redirect output to a directory.

Examples:

% who > names

Redirect standard output to a file named names

% (pwd; ls -l) > out

Redirect output of both commands to a file named out

% pwd; ls -l > out

Redirect output of ls command only to a file named out

Input redirection can be useful, for example, if you have written a FORTRAN program which expects input from the terminal but you want it to read from a file. In the following example, myprog, which was written to read standard input and write standard output, is redirected to read myin and write myout:

% myprog <> myout

You can suppress redirected output and/or errors by sending it to the null device, /dev/null. The example shows redirection of both output and errors:

% who >& /dev/null

To redirect standard error and output to different files, you can use grouping:

% (cat myfile > myout) >& myerror

Bourne Shell Family

The Bourne shell uses a different format for redirection which includes numbers. The numbers refer to the file descriptor numbers (0 standard input, 1 standard output, 2 standard error). For example, 2> redirects file descriptor 2, or standard error. &n is the syntax for redirecting to a specific open file. For example 2>&1 redirects 2 (standard error) to 1 (standard output); if 1 has been redirected to a file, 2 goes there too. Other file descriptor numbers are assigned sequentially to other open files, or can be explicitly referenced in the shell scripts. Some of the forms of redirection for the Bourne shell family are:

Character

Action

>

Redirect standard output

2>

Redirect standard error

2>&1

Redirect standard error to standard output

<

Redirect standard input

|

Pipe standard output to another command

>>

Append to standard output

2>&1|

Pipe standard output and standard error to another command


Note that < and > assume standard input and output, respectively, as the default, so the numbers 0 and 1 can be left off. The form of a command with standard input and output redirection is:

$ command -[options] [arguments] < input file > output file

Redirection may fail under some circumstances: 1) if you have the variable noclobber set and you attempt to redirect output to an existing file without forcing an overwrite, 2) if you redirect output to a file you don't have write access to, and 3) if you redirect output to a directory.

Examples:

$ who > names

Direct standard output to a file named names

$ (pwd; ls -l) > out

Direct output of both commands to a file named out

$ pwd; ls -l > out

Direct output of ls command only to a file named out

Input redirection can be useful if you have written a program which expects input from the terminal and you want to provide it from a file. In the following example, myprog, which was written to read standard input and write standard output, is redirected to read myin and write myout.

$ myprog <> myout

You can suppress redirected output and/or error by sending it to the null device, /dev/null. The example shows redirection of standard error only:

$ who 2> /dev/null

To redirect standard error and output to different files (note that grouping is not necessary in Bourne shell):

$ cat myfile > myout 2> myerror


Source [http://www.mathinfo.u-picardie.fr/asch/f/MeCS/courseware/users/help/general/unix/redirection.html]


Comments

Popular posts from this blog

Cisco Command "Auto secure"

Cisco Command "Auto secure" In today's article, I'm going to quickly inform you about the Privileged EXEC command named "auto secure". Network administrators (like you) use the "auto secure" command to secure the management and forwarding planes of a router. Another way of saying it is, CCNAs use this command to secure a router by disabling common IP services which can be exploited by attackers to initiate network attacks. When the command is typed on a router, it takes the user (ccna) through a command line-interface (CLI) semi-interactive session (which is also known as the AutoSecure dialogue). Below is the command's syntax: auto secure [management | forwarding] [no-interact | full] [ntp | login | ssh | firewall | tcp-intercept] As you can see, the command can use several "optional" keywords: management - This (optional) keyword is used to only secure the management plane of a router. forwarding - This (optional) keyword is used to...

NetSurveyor :: 802.11 (WiFi) Network Discovery / Scanner Tool

NetSurveyor :: 802.11 (WiFi) Network Discovery / Scanner Tool http://www.performancewifi.net/performance-wifi/main/NetSurveyor.htm NetSurveyor is an 802.11 (WiFi) network discovery tool and, as such, its goal in life is to gather information about nearby wireless access points in real time and display it in a useful way. Similar in purpose to NetStumbler , it includes many more features. The data is displayed using a variety of different diagnostic views and charts. Data can be recorded for extended periods and played-back at a later date/time. Also, reports can be generated in Adobe PDF format. Applications for NetSurveyor include the following: During the installation of a wireless network, as an aid in verifying the network is properly configured and antennas are positioned at locations to achieve efficient transmission / reception -- that is, for use in verifying WiFi coverage and maximizing beacon signal strength Trouble-shooting an existing network or wireless environm...

The Seven Habits

The Seven Habits Habit 1: Be Proactive - Changes starts form within; highly effective people make the decisions to improve their lives through the things that they can influence rather than simply reacting to external forces. Habit 2: Begin with the end with mind. - Develop a principle– centered personal mission statement into long- term goals based on personal principle. Habit 3: Put first thing first. - Spend time doing what fits into your personal mission, observing the proper balance between production and building production capacity. Identify the key roles that you take on in life, and make time for each other. Habit 4: Think win / win - Seek agreements and relationships that are mutually beneficial. In cases where a win / win deal cannot be achieved, accept the fact that agreeing to make “no deal”, may be the best alternative. In developing the organizational culture, be sure to reward win / win behavior among employees and avoid inadvertently rewarding win / lose behavior. Habi...