Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of Shell Standard input, output and error

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces the Shell standard input, output and error example analysis, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

File descriptor (fd): the file descriptor is a non-negative integer. When you open an existing file or create a new file, the kernel returns a file descriptor, and you need to use the file descriptor to access the file to read and write the file.

The kernel maintains a record table of files opened by that process for each process. File descriptors are only suitable for Unix and Linux operating systems.

8.1 Standard input, output, and error

File descriptor

Description

Mapping relation

0

Standard input, keyboard

/ dev/stdin-> / proc/self/fd/0

one

Standard output, screen

/ dev/stdout-> / proc/self/fd/1

two

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 appends right input

Error.log

2) overweight directional output

General format: [n] > > word

If n is not specified, the default is 1

Example:

Append the print result to the file: echo "test" > > a.txt

When the bc calculator is not installed, the error output appends the file: echo "1 + 1" | bc2 > error.log

8.4 redirect input

General format: [n] word and > & word are equivalent to > word 2 > & 1

& bind standard output and standard input together and redirect the word file.

Example:

Overwrite to the file when you are not sure whether the execution is right or wrong: echo "1 + 1" | bc & > error.log

Overwrite to the file when you are not sure whether the execution is right or wrong: echo "1 + 1" | bc > error.log 2 > & 1

2) append standard output and standard error

Append format: & > > word is equivalent to > > word2 > & 1

Append files when you are not sure whether the execution is right or wrong: echo "1 + 1" | bc & > > error.log

Redirect standard output and standard input weight to word:

Eof8.6 redirect to empty device

/ dev/null is an empty device, and any array written to it is discarded, but the return status is successful. Correspondingly, there is a / dev/zero device that provides unlimited zero data streams.

We often use the / dev/null device when writing Shell scripts, and output stdout and stderr to it, that is, the data we don't want to output.

Ignore the output by redirecting to / dev/null. For example, if we do not install the bc calculator, we will normally throw an undiscovered command:

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

This allows standards and errors to be output to empty devices.

Ignore standard output:

# echo "test" > / dev/null

Ignore error output:

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

Blog address: http://lizhenliang.blog.51cto.com

QQ group: 323779636 (Shell/Python operation and maintenance development group)

8.7 read command

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

Command format: read [- ers] [- an 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

Backslash is not allowed to escape any characters

S

Hide input

-an array

Save as an array with elements separated by spaces

-d delimiter

Continue to read until the first character of delimiter is encountered to exit

-I text

Use test text as a

-n nchars

Read nchars character return instead of waiting for a newline character

-N nchars

Read nchars characters are returned, except for file Terminator or timeout, other delimiters are ignored

-p prompt

Prompt information

-t timeout

Wait timeout, seconds

-u fd

Specifies the file descriptor number as input. The default is 0.

Name

Variable name

Example:

Get the user input and save it to the variable: # read-p "Please input your name:" VARPlease input your name: lizhenliang# echo $VARlizhenliang user input saved as an array: # read-p "Please input your name:"-an ARRAYPlease input your name: a b c # echo ${ARRAY [*]} a b c encountered e character return: # read-d e VAR123456e# echo $VAR123456 from the file as read standard input: # cat a.txtadfasfd# read VAR < a.txt# echo $VARadfasfdwhile Loop to read each line as standard input for read: # cat a.txt | while read LINE Do echo$LINE; done123abc variable assignment: # read ab C1 23 # echo$ averse echo$ bounded echo$ centering echo 123 | while read abc; do echo "$a $b $c" Done1 2 3 Thank you for reading this article carefully. I hope the article "Shell Standard input, output and sample Analysis of errors" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report