当前位置:网站首页>Quickly understand redirection
Quickly understand redirection
2022-07-19 07:02:00 【I'm Zha Zha Hui】
List of articles
Redirect :
Redirection operators can redirect command input and output data streams from the default device to other locations . The redirection operator itself is not a command , It is a special symbol attached to the command that can change the input and output objects of the command .
Output redirection operator : > 、 >>
Enter the redirection operator : < 、 <<
Input redirection : It refers to reassigning the device to replace the keyboard as a new input device ;
Output redirection : It refers to reassigning the device to replace the display as a new output device .
It is usually the result of the execution of a file or command instead of the keyboard as a new input device , A new output device usually refers to a file .
1、Linux Input redirection
For input redirection , The symbols and functions required are shown in the following table :
| Command symbol format | effect |
|---|---|
| command < file | Use the specified file as the input device for the command |
| command << Delimiter | From a standard input device ( keyboard ) Middle reading , It doesn't stop until it meets the boundary ( The data read in does not include the separator ), The separator here is actually a custom string |
| command < file 1 > file 2 | Will file 1 As an input device for commands , The execution result of this command is output to a file 2 in . |
for example :
By default ,cat The command will accept standard input devices ( keyboard ) The input of , And display to the console , But if you use a file instead of a keyboard as an input device , Then the command will take the specified file as the input device , And read and display the contents of the file to the console .
[[email protected] ~]# cat < /etc/passwd
1
The output follows [[email protected] ~]# cat /etc/passwd equally , Only this part of the code is based on /etc/passwd File as input device .
Another example is :
[[email protected] ~]# cat << 0
// Here's the input ( belt > Number )
>c.biancheng.net
>Linux
>0
// The console prints out the information just entered
c.biancheng.net
Linux.
As can be seen from the above code , When specifying the 0 As a boundary , As long as you don't enter 0, You can input data all the time .
But on the whole , Input redirection is rarely used .
2、Linux Output redirection
The frequency of output redirection is higher , Unlike input redirection , Output redirection can also be subdivided into standard output redirection and error output redirection .
The symbols and functions used for output redirection are as follows :
| Command symbol format | effect |
|---|---|
| command > file | Redirect the standard output result of the command execution to the specified file , If the file already contains data , The original data will be cleared , Write new data again . |
| command 2> file | Redirect the error output of the command execution to the specified file , If the file already contains data , The original data will be cleared , Write new data again . |
| command >> file | Redirect the standard output result of the command execution to the specified file , If the file already contains data , The new data will be written to the back of the original content . |
| command 2>> file | Redirect the error output of the command execution to the specified file , If the file already contains data , The new data will be written to the back of the original content . |
| command >> file 2>&1 Or order &>> file | Write standard output or error output to the specified file , If the file already contains data , The new data will be written to the back of the original content . Be careful , In the first format , final 2>&1 Is one. , It can be considered as fixed writing . |
| for example : |
$ ls -l > lsoutput.txt
1
This order is very important ls The output of the command is saved to a file lsoutput.txt in .
$ ps >> lsoutput.txt
1
This order is very important ps The output of the command is appended to lsoutput.txt End of file .
for example
>
[[email protected] ~]# echo 123 > file
[[email protected] ~]# more file
123
>>
[[email protected] ~]# echo 456 >> file
[[email protected] ~]# more file
123
456
2>
[[email protected] ~]# 123 2> file
[[email protected] ~]# more file
bash: 123: command not found...
2>>
[[email protected] ~]# 456 2> file
[[email protected] ~]# 123 2>> file
[[email protected] ~]# more file
bash: 456: command not found...
bash: 123: command not found...
&>
[[email protected] ~]# echo 123 &> fule
[[email protected] ~]# more fule
123
[[email protected] ~]# 123 &> fule
[[email protected] ~]# more fule
bash: 123: command not found...
>&
[[email protected] ~]# 123 >& fule
[[email protected] ~]# echo 123 >& fule
take 1 Redirect to file , take 2 Redirect to 1
[[email protected] ~]# echo 123 1> file 2>&1
[[email protected] ~]# more file
123
take 2 Redirect to file , take 1 Redirect to 2
[[email protected] ~]# 123 >& fule
[[email protected] ~]# echo 123 >& fule
bash: 123: command not found...
Redirect files to 0
bash: 123: command not found...
cat Read content from standard input , Then output to a file
[[email protected] ~]# cat < file &>> file
[[email protected] ~]# more file
bash: 123: command not found...
cat: -: input file is output file
[[email protected] ~]# cat < file &>> fule
[[email protected] ~]# more fule
123
bash: 123: command not found...
cat: -: input file is output file
cat: -: input file is output file
expand
Complex usage of redirection using file descriptors
Redirection using file descriptors uses & Symbol :
cmd >&n Send the output to the file descriptor n
cmd m>&n Output to file symbol m To the file descriptor ncmd >&- Turn off standard output
cmd <&n Input from file descriptor n
cmd m<&n m From the file descriptor n
cmd <&- Turn off standard input
cmd <&n- Move the input file descriptor n Instead of copying it
cmd >&n- Move the output file descriptor n Instead of copying it
边栏推荐
猜你喜欢

F5ltm (I) logic diagram

Ucloud Shanghai arm cloud server evaluation

小迪网络安全-笔记(3)

Tianyi cloud Hangzhou virtual machine (VPS) performance evaluation
![[ restartedMain] o.s.b.d.LoggingFailureAnalysisReporter :](/img/dd/054af819c8bdca31bd135495386fb4.png)
[ restartedMain] o.s.b.d.LoggingFailureAnalysisReporter :

SSH remote login service

内网穿透服务器搭建教程,NPS使用教程

5G时代服务器在这里面起着什么作用?

Comparison of advantages and disadvantages between SD NAND and EMMC

Utilisation et différenciation des dictionnaires, des tuples et des listes,
随机推荐
Application case of CS brand SD NAND in air quality inspection industry
UCloud(优刻得) 上海 ARM 云服务器评测
FreeBSD 12 domestic source speed up PKG and ports take a note
类与super、继承
可怜小凡(模拟)
Freebsd 12更换启动界面背景
类与对象
Wireshark packet capturing: error analysis
递归访问目录,打印斐波那契数列,高阶函数
Performance test and price comparison of cloud servers of Alibaba cloud, Tencent cloud, Huawei cloud, ucloud and Tianyi cloud
Slag learning path (1) output the calendar page of a certain month of a certain year
逃出湖心(数学题)
Gnome 安装扩展插件(40.1版本,opensuse tumbleweed)。
[ restartedMain] o.s.b.d.LoggingFailureAnalysisReporter :
Weight matching (greedy)
Xiaodi network security - Notes (5)
Entry level of simple irules writing
SYN洪水攻击的原理,syn洪水攻击的解决办法
pytorch张量
Personal information management system