Shell Base 02 standard file descriptor Stdin,stdout,stderr and input-output redirection

Source: Internet
Author: User
Tags stdin

File descriptor abbreviation Description

0 STDIN Standard Input

1 STDOUT Standard output

2 STDERR standard Error

1. STDIN

can come from the keyboard and can be entered from a redirected file.

1 Cat 2 a      #从键盘输入 3a      #输出 4b      #从键盘输入 5b      #输出  6 ^c    #ctrl +c forced termination input
View Code
1[Email protected] documents]$Cat<Test2#!/bin/Bash3 a4 b5 C6[Email protected] documents]$CatTest7#!/bin/Bash8 a9 bTen C One [email protected] documents]$ A  -#采用文件重定向输入
View Code

2. STDOUT

1[Email protected] documents]$ls-L >ltest2[Email protected] documents]$Catltest3Total +4Drwxrwxr-x.2Hermioner Hermioner6Jul -  Geneva: -a5-rw-rw-r--.1Hermioner Hermioner0Jul -  -: inltest6-rw-rw-r--.1Hermioner Hermioner -Jul -  -: -Test7-rwxrwxr-x.1Hermioner Hermioner -Jul -  .: Wutest18-rwxrwxr-x.1Hermioner Hermioner196Jul -  +: toTest1.SH9-rw-rw-r--.1Hermioner Hermioner theJul -  .: Abouttest2Ten-rwxrwxr-x.1Hermioner Hermioner144Jul -  to: ATest2.SH One-rw-rw-r--.1Hermioner Hermioner theJul -  to: $test3 A-rwxrwxr-x.1Hermioner Hermioner94Jul -  .: -Test3.SH --rwxrwxr-x.1Hermioner Hermioner221Jul -  .: ,testfile - [email protected] documents]$ the  -  -#本该到显示器的输出重定向了文件, standard output is the terminal display
View Code
1 [[email protected] documents]$ lss-l > atest2bash:lss:command not found ... 3 ' ls ' 4 Cat atest 5  6 #当错误命令重定向时, found only in the terminal error, nothing in the file is written. can use stderr to catch errors
View Code

3. STDERR

By default, stderr and stdout are output to the terminal, even if redirection does not change the output of the stderr. Example of the above LSS command. However, for the sake of error separation, the following workaround can still be implemented:

(1) REDIRECT only error

Based on the summary of the symbols in the table, the Stderror file descriptor is changed to 2 and can only redirect errors.

1 2> Test2cat  test3bash:lss:command not found ... 4 ' ls ' 5
View Code

(2) redirect Data and errors

Data and errors can be divided into open or put together.

1[Email protected] documents]$ls 2 a ltest test1 test2 test3 testfile3Atest test Test1.SHTest2.SHTest3.SH4[Email protected] documents]$ls-L test1 test2 y2> Test81>Test95[Email protected] documents]$CatTest86 ls: Cannot access y:no suchfileor directory7[Email protected] documents]$CatTest98-rwxrwxr-x.1Hermioner Hermioner -Jul -  .: Wutest19-rw-rw-r--.1Hermioner Hermioner theJul -  .: Abouttest2Ten [hermion[email protected] documents]$ One  A#这样错误和数据分别保存在了两个文件中
View Code

4. Input and output redirection

In a nutshell, output redirection is the output of the output from the command window to the specified file, and the input redirection is the command window where the content that was supposed to be entered into the file was obtained.

There are mainly the following four:

Command > OutputFile

Command >> outputfile Append output

Command < Inputfile

Command << inputfile Append write

Note: You can also customize the redirect descriptor, such as the 3> file, to specify the redirection form of file, which matches the symbol 3. Bash Shell allows you to create your own file descriptors in a script. You can create file descriptor 3-9 and assign them to any output file that you want to use. Once you have created the file descriptor, you can redirect the output of any command to there using standard redirection symbols. Input in the same vein.

5. Tee----------Message Logging

The tee command facilitates the simultaneous sending of output to standard output and log files. This allows you to display script messages on the monitor and save them in a log file.

1 Date Tee testfile 2  - £ º20183cat  testfile 4  £ º20185 
View Code

Reference Documents :

Linux command line and Shell Scripting Encyclopedia (3rd edition) [Mei] Bloom (Richard Blum), Bresnahan (Christine Bresnahan), Jia, Wuhai

Shell Base 02 standard file descriptor Stdin,stdout,stderr and 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.