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

How to realize data redirection by shell in linux

2025-01-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

This article mainly shows you "how to realize data redirection in shell in linux", the content is simple and easy to understand, and the organization is clear. I hope it can help you solve your doubts. Let Xiaobian lead you to study and learn "how to realize data redirection in shell in linux" this article.

Linux file descriptor: can be understood as linux tracking open files, and allocated a number, this number is a bit similar to the c language when operating the file handle, through the handle can achieve file reading and writing operations. The user can customize the file descriptor range is: 3-num, the maximum number, with the user's: ulimit-n definition number, can not exceed the maximum value.

After Linux starts, it will open three file descriptors by default, namely: standard input 0, correct output standard output 1, error output error 2.

After opening the file. New file binding descriptors can be added sequentially. A shell command inherits the file descriptor of the parent process. Therefore, all shell commands run with a default of three file descriptors.

For any linux command execution, it would be a process like this:

An order was executed:

First there is an input: input can be from the keyboard or from a file

Command execution completed: successful, will output the successful result to the screen: standard output default is screen

command execution error: error will also be output to the screen above: standard error default also refers to the screen

File input and output are accomplished by tracing integer handles to all open files for a given process. These numeric values are file descriptors. The best-known file meter descriptors are stdin, stdout and stderr, and the file descriptor numbers are 0, 1 and 2, respectively. These numbers and their respective devices are reserved. Before a command is executed, all inputs and outputs will be prepared first. By default, they will be bound separately (stdin,stdout,stderr). If an error occurs at this time, the command will terminate and will not be executed. Command parsing process, you can refer to: Linux Shell wildcard, metacharacters, escape characters using examples

These default outputs and inputs are all default in linux, and sometimes we don't want to output the results to the screen during use. I want to export to a file or other device. At this point we need to redirect the output.

Common input and output operators under Linux shell are:

1. Standard input (stdin): code 0, using

< 或 /proc/self/fd/0 0代表:/dev/stdin 2. 标准输出 (stdout):代码为 1 ,使用 >

or>> ; /dev/stdout -> /proc/self/fd/1 1 stands for: /dev/stdout

3. Standard error output (stderr): code 2, use 2> or 2>> ; /dev/stderr -> /proc/self/fd/2 2 stands for: /dev/stderr

Output redirection:

Format:

command-line1 [1-n] > file or file operator or device

The above command means: redirect the result of a command execution (standard output, or error output, both intended to print to the screen) to another output device (file, open file operator, printer, etc.) 1, 2 standard output, error output, respectively.

Examples:

The code is as follows:

#Show current directory file test.sh test1.sh test1.sh does not exist

[chengmo@centos5 shell]$ ls test.sh test1.sh

ls: test1.sh: No such file or directory

test.sh

#The correct output and the error output are displayed on the screen, now you need to write the correct output to suc.txt

# 1> can be omitted, do not write, default to standard output

[chengmo@centos5 shell]$ ls test.sh test1.sh 1>suc.txt

ls: test1.sh: No such file or directory

[chengmo@centos5 shell]$ cat suc.txt

test.sh

#Output error, not output to screen, output to err.txt

[chengmo@centos5 shell]$ ls test.sh test1.sh 1>suc.txt 2>err.txt

[chengmo@centos5 shell]$ cat suc.txt err.txt

test.sh

ls: test1.sh: No such file or directory

#Continue appending the output to suc.txt err.txt ">>" appending operators

[chengmo@centos5 shell]$ ls test.sh test1.sh 1>>suc.txt 2>>err.txt

#Turn off error output messages

[chengmo@centos5 shell]$ ls test.sh test1.sh 2>&-

test.sh

[chengmo@centos5 shell]$ ls test.sh test1.sh 2>/dev/null

test.sh

#&[n] represents an existing file descriptor,&1 represents output &2 represents error output &-represents closing the descriptor bound to it

#/dev/null This device is a black hole device in Linux. Any information output to this device will be eaten.

#Close all outputs

[chengmo@centos5 shell]$ ls test.sh test1.sh 1>&- 2>&-

#Close 1, 2 file descriptors

[chengmo@centos5 shell]$ ls test.sh test1.sh 2>/dev/null 1>/dev/null

#Forward 1,2 output to/dev/null device

[chengmo@centos5 shell]$ ls test.sh test1.sh >/dev/null 2>&1

#Binding error output 2 to correct output 1, and then sending correct output to/dev/null device is common

[chengmo@centos5 shell]$ ls test.sh test1.sh &>/dev/null

#& stands for standard output, error output Input all standard output and error output to/dev/null file

Note:

1. When the shell encounters the ">" operator, it will determine whether the file on the right exists. If it exists, delete it first and create a new file. There is no direct creation. Whether the command on the left is successful or not. The right file will be empty.

2,">>" operator, judge the right file, if it does not exist, create it first. Opening a file in Add mode assigns a file descriptor [unspecified, defaults to 1,2] and then binds to the standard output (1) or error output (2) on the left.

3. When the command: is executed, the descriptor of the binding file is automatically invalid. 0, 1, 2 will be free again.

4, a command to start, command input, correct output, error output, default binding 0, 1, 2 file descriptors respectively.

5. Before a command is executed, check whether the output is correct. If the output device is wrong, the command will not be executed.

input redirection

Format:

command-line [n] catfile

testing

cat file test

#here press [ctrl]+d to leave

#Get data from standard input keyboard and output to catfile file

[chengmo@centos5 shell]$ cat>catfile catfile &6

#Restore standard output

[chengmo@centos5 shell]$ exec 6>&-

#Close fd 6 descriptor

[chengmo@centos5 ~]$ ls /proc/self/fd/

0 1 2 3

Note: Save standard input to file descriptor 6 before use. Here, file descriptors will be opened by default 0,1,2. Custom descriptors can also be used. Standard output is then bound to a file, and all output then occurs to a file. When finished, restore standard output and close Open File Descriptor 6.

Interesting things:

Some friends may use: exec1> suc.txt, then all the output is bound to the suc.txt file, so how to restore the original? Try it and you'll see what the problem is...

Complicated examples

The code is as follows:

exec 3test.sh;

#Open test.sh for read-write operations, bound to file descriptor 3

while read line&-

exec 3

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

Servers

Wechat

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

12
Report