Shell script Programming shell input/output redirection

Source: Internet
Author: User
Tags stdin

1. In most cases, UNIX system commands accept input from your terminal and send the resulting output back to your terminal

The list of REDIRECT commands is as follows:

2. Output redirection: Redirects are generally implemented by inserting specific symbols between commands;

#!/bin/> file1# above this command executes Command1 and then stores the output to file1# note that any content that already exists in the file1 will be replaced by the new content. If you want to add new content to the end of the file, use the >> operator

(1) Example:

Who > Users

After execution, there is no output information at the terminal, because the output has been redirected from the default standard output device (terminal) to the specified file (users)

Operation Result:

output redirection overrides the file contents , see the following example:

If you do not want the file content to be overwritten, you can use >> to append to the end of the file, for example:

3. Input redirection: As with output redirection, UNIX commands can also get input from a file

< file1# this way, commands that would otherwise need to get input from the keyboard are transferred to the file read content. #注意: Output redirection is greater than (>), and input redirection is less than sign (<).

Instance:

WC-L users   " input redirect Test"   

Wc-l < users//The first example, the file name is output, the second one does not, because it only knows what to read from the standard input

4. Replace input and output at the same time, execute Command1, read content from File infile, and write to file outfile

#command1 < infile > outfile

5. Redirect in-depth explanation:

In general, each unix/linux command will open three files when it is run:

(1) standard input file (stdin): stdin file descriptor for 0,unix program reads data from stdin by default

(2) Standard output file (stdout): StdOut file descriptor is 1,unix program defaults to stdout output data

(3) standard error file (stderr): stderr file descriptor is 2,unix program writes error message to STDERR stream

By default, Command1 > file redirects stdout to file command < file to redirect stdin to file

(1) If you want stderr to be redirected to file, you can write:

2 >2 >> file   // If you want stderr appended to the end of file  2 Standard offset file (stderr)

(2) If you want to redirect stdout and stderr to file after merging, you can write:

2>&12>&1

(3) If you want both stdin and stdout to be redirected, you can write:

Command < file1 > file2      //command commands redirect stdin to File1, redirect stdout to File2

A special redirect in 6.Here Document:shell to redirect input to an interactive shell script or program

#它的基本的形式如下::<<!  << delimiter  documentdelimiter!  Content (document) between the #它的作用是将两个 delimiter is passed as input to command

(1) At the end of the delimiter must be shelf write, the front cannot have any characters, the back can not have any characters, including spaces and tab indentation

(2) The space before and after the beginning of the delimiter is ignored

Instance:

Wc-l << EOF    Welcome to    Rookie Tutorial    Www.runoob.comEOF

Operation Result:

7./dev/null file: A special file, the content written to it will be discarded, if you try to read from the file, will not read anything, will play a "no output" effect

If you want to execute a command, but you do not want the output to appear on the screen, you can redirect the output to/dev/null:

#command >/dev/null

If you want to block stdout and stderr, you can write this:

#command >/dev/null2>&1

Shell script Programming shell input/output redirection

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.