Shell standard input, output, and error

Source: Internet
Author: User

Security code: Taohuatan depth thousand feet, less than mountain send Me love.

File descriptor (FD): The file descriptor is a non-negative integer, and when you open an existing file or create a new file, the kernel returns a

File descriptors, and read-write files also need to use file descriptors to access files.

The kernel maintains the file record tables that the process opens for each process. File descriptors are only suitable for Unix, Linux operating systems.

8.1 standard input, output, and error

File Descriptor Description Mapping relationship

0 standard input, keyboard/dev/stdin-/proc/self/fd/0

1 standard output, screen/dev/stdout-/PROC/SELF/FD/1

2 standard error, screen/dev/stderr-/PROC/SELF/FD/2

8.2 redirect Symbol

Symbol description

> symbol left output as right input (standard output)

>> symbol left output append right input

< symbol right output as left input (standard input)

<< symbol Right output append left input

& REDIRECT Binding symbols

The input and output can be interpreted to the shell by a redirect symbol.

The shell command executes commands from left to right.

The following n letters are file descriptors.

8.3 redirect Output

1) Overwrite output

General format: [N]>word

If n is not specified, the default is 1

Example:

Print results written to file: echo "Test" > A.txt

When the BC Calculator is not installed, the error output is written to the file: echo "1 + 1" |BC 2> Error.log

2) Append redirect Output

General format: [N]>>word

If n is not specified, the default is 1

Example:

Print results append to file: echo "Test" >> a.txt

Error output append file when no BC calculator is installed: echo "1 + 1" |BC 2> Error.log

8.4 4 REDIRECT Input

General format: [N]<word

If n is not specified, the default is 0

Example:

A.txt content as grep input: grep "Test"--color < A.txt

8.5 5 redirect standard output and standard error

1) Overwrite redirection standard output and standard error

Both formats redirect standard output and standard error:

&>word and >&word are equivalent to >word 2>&1

& the standard output and standard input are bound together to redirect Word files.

Example:

When not sure execution is overwritten to the file: echo "1 + 1" |BC &> Error.log

When not sure execution is overwritten to the file: echo "1 + 1" |BC > Error.log 2>&1

2) append standard output and standard error

Append format: &>>word equivalent to >>word 2>&1

Append files when unsure of execution: echo "1 + 1" |BC &>> Error.log

REDIRECT standard output and standard standard input append to Word:

<<[-]word

Here-document

Delimiter

Reads the input source from the current shell until it encounters a row that contains only delimiter termination and the content as standard input.

The EOF standard input is then written to A.txt as the cat standard output:

# Cat <<eof

123

Abc

Eof

123

Abc

# cat > A.txt << EOF

> 123

> ABC

> EOF

8.6 Redirect to Empty device

/dev/null is an empty device, and the array to which it is written is discarded, but the return status is successful. And there's a corresponding

/dev/zero devices, providing unlimited 0 of data streams.

We often use/dev/null devices when writing Shell scripts, outputting stdout and stderr to it, that is, we do not want to

The data for these outputs.

Ignoring the output by redirecting to/dev/null, for example, we did not install the BC calculator, normally throws no Discovery command:

# echo "1 + 1" |BC >/dev/null 2>&1

This lets the standard and error output to an empty device.

Ignore standard output:

# echo "Test" >/dev/null

Ignore error Output:

# echo "1 + 1" |BC 2>/dev/null

D 8.7 Read command

The read command reads from the standard input and copies the contents of the input to the variable.

Command format: read [-ers] [-a array] [-D delim] [-I text] [-N nchars] [-N nchars] [-P

Prompt] [-t timeout] [-u fd] [name ...]

-e use ReadLine to get rows in an interactive shell

-R does not allow a backslash to escape any characters

-S hidden input

-a array is saved as an array, and the elements are separated by spaces

-D delimiter continues to read until the first character of delimiter encounters exits

-I text takes the test text as

-N Nchars reads nchars characters back instead of waiting for line breaks

-N Nchars reads nchars characters, except when a file terminator or timeout is encountered, the other separators are ignored

-P Prompt Hint information

-T timeout waiting time out, seconds

-u FD Specifies the file description symbol code as input, default is 0

Name Variable name

Example:

Get user input saved to variable:

# read-p "Please input your name:" VAR

Please input your Name:lizhenliang

# echo $VAR

Lizhenliang

User input is saved to an array:

# read-p "Please input your name:"-A ARRAY

Please input your name:a b C

# echo ${array[*]}

A b C

Encountered e-character return:

# read-d E VAR

123

456

E

# echo $VAR

123 456

From the file as read standard input:

# Cat A.txt

Adfasfd

# Read VAR < A.txt

# echo $VAR

Adfasfd

The while loop reads each row as the standard input for read:

# cat A.txt |while read line; do Echo $LINE; Done

123

Abc

Assign values to each variable:

# read a b C

1 2 3

# echo $a

1

# echo $b

2

# echo $c

3

# echo 1 2 3 | While read a B c;do echo "$a $b $c"; Done

1 2 3

Thank you for watching, sincerely hope to help you!

This article from "A Candle" blog, declined reprint!

Shell standard input, output, and error

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.