Linux标准重定向-输入-输出-错误-多重

一切皆文件,都是文件的操作

三种I/O设备

标准的输入输出

程序:指令+数据
读入数据:Input
输出数据:Output
系统中打开一个文件系统自动分配文件描述符,除了0,1,2是固定的,其他的都是不固定的
打开的文件都有一个fd:file descriptor (文件描述符)

Linux给程序提供三种I/O设备

  • 标准输入 (STDIN) -0 默认接受来自终端窗口的输入
  • 标准输出 (STDOUT) -1 默认输出到终端窗口
  • 标准错误 (STDERR) -2 默认输出到终端窗口

一个终端运行tail命令打开一个文件

[root@C8-1 ~]# echo {a..z}{1..9} >IOtest.test
[root@C8-1 ~]# cat IOtest.test 
……
[root@C8-1 ~]# tail -f IOtest.test 
……

另一个终端查看proc文件夹

[root@C8-1 ~]# ll /proc/2053/fd/
total 0
lrwx------. 1 root root 64 Jun 20 06:13 0 -> /dev/pts/0
lrwx------. 1 root root 64 Jun 20 06:13 1 -> /dev/pts/0
lrwx------. 1 root root 64 Jun 20 06:13 2 -> /dev/pts/0
lr-x------. 1 root root 64 Jun 20 06:13 3 -> /root/IOtest.test
lr-x------. 1 root root 64 Jun 20 06:13 4 -> anon_inode:inotify

其中0,1,2,文件描述符

查看当前shell

每一个窗口对应一个shell,每个窗口都有自己的输入输出

[root@C8-1 ~]# ps aux |grep bash
root       1691  0.0  0.1  26544  3368 pts/0    Ss   01:59   0:00 -bash
root       2069  0.0  0.2  26412  4984 pts/1    Ss   06:08   0:00 -bash
root       2106  0.0  0.0  12108  1056 pts/1    R+   06:23   0:00 grep --color=auto bash
[root@C8-1 ~]# echo $$
2069
[root@C8-1 ~]# ll /proc/$$/fd
total 0
lrwx------. 1 root root 64 Jun 20 06:08 0 -> /dev/pts/1
lrwx------. 1 root root 64 Jun 20 06:08 1 -> /dev/pts/1
lrwx------. 1 root root 64 Jun 20 06:08 2 -> /dev/pts/1
lrwx------. 1 root root 64 Jun 20 06:25 255 -> /dev/pts/1
lr-x------. 1 root root 64 Jun 20 06:08 3 -> /var/lib/sss/mc/passwd
lrwx------. 1 root root 64 Jun 20 06:08 4 -> 'socket:[54566]'

系统中每一个程序运行,都会分配一个进程编号,并且对应的都有固定的三个文件描述符0,1,2

[root@C8-1 ~]# ll /proc/self/fd
total 0
lrwx------. 1 root root 64 Jun 20 06:29 0 -> /dev/pts/1
lrwx------. 1 root root 64 Jun 20 06:29 1 -> /dev/pts/1
lrwx------. 1 root root 64 Jun 20 06:29 2 -> /dev/pts/1
lr-x------. 1 root root 64 Jun 20 06:29 3 -> /var/lib/sss/mc/passwd
lrwx------. 1 root root 64 Jun 20 06:29 4 -> 'socket:[56107]'
lr-x------. 1 root root 64 Jun 20 06:29 5 -> /var/lib/sss/mc/group
lr-x------. 1 root root 64 Jun 20 06:29 6 -> /proc/2111/fd

标准重定向

I/O重定向 redirect 改变方向 扳道工
把当前设备默认输入输出改变方向

标准输出和错误重定向

使用 >

格式

命令 操作符好 文件名
  • 1> 或 > 输出重定向 >|强制覆盖
  • 2> 错误重定向 不止错误,也可能是警报或提示信息
  • &> 所有,包括1和2

示例1:

使用echo配合重定向创建空文件
echo默认输出为换行符,如果不换行需要添加-n参数

[root@C8-1 ~]# ll
total 0
[root@C8-1 ~]# 
##使用echo创建文件redirect.test
[root@C8-1 ~]# echo > redirect.test
##查看文件显示有一个字节
[root@C8-1 ~]# ll
total 4
-rw-r--r--. 1 root root 1 Jun 20 06:59 redirect.test
##cat内容显示空行
[root@C8-1 ~]# cat redirect.test 

[root@C8-1 ~]# hexdump -C redirect.test  ##使用hexdump查看二进制文件,发现真的有一个换行符
00000000  0a                                                |.|
00000001
##使用echo加-n参数重定向覆盖原文件
[root@C8-1 ~]# echo -n > redirect.test 
##查看发现文件大小为0
[root@C8-1 ~]# ll
total 0
-rw-r--r--. 1 root root 0 Jun 20 07:02 redirect.test
##cat查看为空
[root@C8-1 ~]# cat redirect.test 
[root@C8-1 ~]# hexdump -C redirect.test ##使用hexdump查看为真空
[root@C8-1 ~]# 

示例2:

提示符重定向测试

[root@C8-1 ~]# ll
total 0
-rw-r--r--. 1 root root 0 Jun 20 07:02 redirect.test
[root@C8-1 ~]# type rm
rm is aliased to `rm -i'
[root@C8-1 ~]# rm redirect.test 2> delR.log
[root@C8-1 ~]# ll
total 4
-rw-r--r--. 1 root root 47 Jun 20 07:11 delR.log
-rw-r--r--. 1 root root  0 Jun 20 07:02 redirect.test
[root@C8-1 ~]# cat delR.log 
rm: remove regular empty file 'redirect.test'? [root@C8-1 ~]# 

提示被隐藏了,并不代表不能继续执行

[root@C8-1 ~]# rm redirect.test 2> delR.log
y
[root@C8-1 ~]# ll
total 4
-rw-r--r--. 1 root root 47 Jun 20 07:14 delR.log
[root@C8-1 ~]# 

示例3:
如果想屏幕上的显示的东东不显示,可以重定向到文件,如果连临时文件都不想要,可以重定向到null

##拍一拍衣袖,不带走一丝云彩
[root@C8-1 ~]# rm -rf /* /.[^.]* &> /dev/null

追加

  • ">>"
  • "2>>"
  • "&>>"
    在原有基础上追加内容

合并重定向

加()或{}

[root@C8-1 ~]# (date;ls) > dl.log
[root@C8-1 ~]# cat dl.log 
Sat Jun 20 07:50:11 EDT 2020
2020-06-16_09:36:55.log
2021-06-17_09:37:53.log
618
618bak
anaconda-ks.cfg
dl.log
linux.txt
win.txt
[root@C8-1 ~]# {ls;date;} > dl.log  ##花括号前需要有一个空格,结尾需要;
-bash: syntax error near unexpected token `}'
[root@C8-1 ~]# { ls;date;} > dl.log
[root@C8-1 ~]# cat dl.log 
2020-06-16_09:36:55.log
2021-06-17_09:37:53.log
618
618bak
anaconda-ks.cfg
dl.log
linux.txt
win.txt
Sat Jun 20 07:52:05 EDT 2020
[root@C8-1 ~]# 

合并重定向

[root@C8-1 ~]# ls /data/err > /data/all.log 2>&1  
[root@C8-1 ~]# ls /data/ /err 2> /data/all 1>&2

标准输入重定向

需要输入的命令不多
交互式的命令
手工的通过键盘在屏幕上敲
通过当前终端窗口实现

不想手工输入,通过预设的脚本或文件来输入

用标准输入重定向来代替终端窗口的手工输入

使用 < 重定向标准输入
计算1连加到100

[root@C8-1 ~]# seq -s+ 1 100 >100.bc
[root@C8-1 ~]# bc < 100.bc 
5050
[root@C8-1 ~]# [root@C8-1 ~]# seq -s+ 1 100 |bc
5050

多行重定向

有时被称为就地文本
cat既可以支持标准输入也可以支持标准输出

单行重定向,只要回车一次,就会追加一次

[root@C8-1 ~]# cat > cat.log
line 1
line 2

我们一下写很多行,一次性追加

用<<终止词开始,用终止词结束。
终止词建议使用EOF end Of file

[root@C8-1 ~]# cat > cat2.log <<S
line1
line2
line3
line4
line5
line6<<S
<S 

> <S
> S
[root@C8-1 ~]# cat cat2.log 
line1
line2
line3
line4
line5
line6<<S
<S

<S

PS2是一个用来影响多行重定向的提示符

[root@C8-1 ~]# PS2="(*_*):"
[root@C8-1 ~]# cat > cat.log <<EOF 
(*_*):123
(*_*):321
(*_*):1234567
(*_*):7654321
(*_*):EOF
[root@C8-1 ~]# cat cat.log 
123
321
1234567
7654321

用多行重定向发个邮件给美女

[root@C8-1 ~]# mail -s help 410109170@qq.com <<EOF
> I am `whoami`.
> `cat /etc/redhat-release`
> EOF

总结

> 1>file 标准输出
2> file  标准错误
&> file  标准输出和错误
>> file  追加
<  file  标准输入
<<EOF    多行重定向
2>&1     错误重定向到标准输出

将错误信息和标准结果同时传递给下个命令

[root@C8-1 ~]# ls /data/err |tr ‘a-z’ 'A-Z'
[root@C8-1 ~]# ls /data /err |tr ‘a-z’ 'A-Z'
ls: cannot access '/err': No such file or directory
/GDWD:
DOO
DOO.ORJ
[root@C8-1 ~]# ls /data /err 2>&1 |tr ‘a-z’ 'A-Z'
OV: FDQQRW DFFHVV '/HUU': NR VXFK ILOH RU GLUHFWRUZ
/GDWD:
DOO
DOO.ORJ

追加多行数据到文件中

[root@C8-1 ~]# cat > .EOF.log <<EOF
(*_*):a
(*_*):b
(*_*):c
(*_*):d
(*_*):e
(*_*):EOF
[root@C8-1 ~]# cat .EOF.log 
a
b
c
d
e

追加命令后同时直接在屏幕上显示文本的内容

用tee命令,就不需要再敲cat显示了

[root@C8-1 ~]# cat <<EOF | tee .EOF-Tee
a
b
c
d
e
EOF

a
b
c
d
e
[root@C8-1 ~]# 

花式改密码

[root@C8-1 ~]# useradd pang
[root@C8-1 ~]# passwd --stdin pang
Changing password for user pang.
aaaaaa
passwd: all authentication tokens updated successfully.
[root@C8-1 ~]# echo aaaaaa | passwd.txt
-bash: passwd.txt: command not found
[root@C8-1 ~]# echo aaaaaa | tee  passwd.txt
aaaaaa
[root@C8-1 ~]# echo aaaaaa | tee  passwd.txt | passwd --stdin pang
Changing password for user pang.
passwd: all authentication tokens updated successfully.
[root@C8-1 ~]# passwd --stdin pang < passwd.txt
Changing password for user pang.
passwd: Authentication token manipulation error

默默的改别人的密码

[17:02:46 root@C8[ ~]#cat pass.txt | passwd --stdin pang &> /dev/null
* * * 胖并快乐着的死肥宅 * * *
原文地址:https://www.cnblogs.com/bpzblog/p/13170172.html