文章目录

展开

输入输出

重定向

重定向

什么是重定向?

将原本要输出到屏幕上的内容,重新输入到其他设备中

为什么要学重定向?

# 1.输出的内容,比较重要的时候,我们想把它保存到文件中
# 2.在后台执行的程序,我不想让它输出的内容,干扰到屏幕
# 3.将定时任务的结果保存下来(备份,是否成功)
# 4.一些执行的命令,知道它有可能会有错误输出,但是不想看错误输出
# 5.执行一个命令,可能报错和正确的输出并存,类似错误日志与标准正确日志需要分别输出至不同的文件。

命令返回值(拓展)

如何判断一个命令是否执行成功?????
$? 命令的返回值
返回值是0,则代表上一条命令执行成功
返回值非0,则代表上一条命令执行不成功
[root@web ~]# ll
total 24
-rw-r--r-- 2 root root    8 Jul  3 16:01 123.txt
drwxr-xr-x 3 root root   40 Jul  2 22:42 hg
-rw-r--r-- 1 root root  260 Jul  2 23:03 ip.sh
-rw-r--r-- 1 root root  421 Jul  3 11:56 jiaohu.sh
-rwxr-xr-x 1 root root 1488 Jul  3 10:54 jump.sh
drwxr-xr-x 2 root root    6 Jul  3 16:04 ml
-rw-r--r-- 1 root root  219 Jul  3 11:39 ttb.sh
-rw-r--r-- 1 root root 1218 Jul  3 12:24 tt.sh
drwxr-xr-x 2 root root  104 Jul  5 17:41 tu
[root@web ~]# $?
-bash: 0: command not found
成功
[root@web ~]# ll 123
ls: cannot access 123: No such file or directory
[root@web ~]# $?
-bash: 2: command not found
失败

输入输出

文件描述符

名称 文件描述符 作用
stdin 0 标准输入
stdout 1 标准输出
stderr 2 错误输出
文件名 3+

img

[root@web ~]# ping baidu.com >> /tmp/1.txt &
[1] 67423
[root@web ~]# ps -ef|grep ping
root      67423  65010  0 19:13 pts/3    00:00:00 ping baidu.com
root      67569  65010  0 19:13 pts/3    00:00:00 grep --color=auto ping         
[root@web ~]# ll /proc/67423/fd
total 0
lrwx------ 1 root root 64 Jul  5 19:13 0 -> /dev/pts/3
l-wx------ 1 root root 64 Jul  5 19:13 1 -> /tmp/1.txt
lrwx------ 1 root root 64 Jul  5 19:13 2 -> /dev/pts/3
lrwx------ 1 root root 64 Jul  5 19:13 3 -> socket:[1885801]

输入输出符号

名称 符号
标准输入重定向 < 或者 0< 将符号右边的内容交给符号左边的命令
<< 或者 0<<
标准输出覆盖重定向 > 或者 1> 将原本要输出在屏幕上的正确内容,覆盖到重定向的文件中
标准输出追加重定向 >> 或者1>> 将原本要输出在屏幕上的正确内容,追加到重定向的文件中
错误输出覆盖重定向 2> 将原本要输出在屏幕上的错误内容,覆盖到重定向的文件中
错误输出追加重定向 2>> 将原本要输出在屏幕上的错误内容,追加到重定向的文件中

案例1:标准输出重定向(覆盖)

img

[root@web ~]# ifconfig > abc
[root@web ~]# cat abc
br-c96a457c8696: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.250.1  netmask 255.255.255.0  broadcast 192.168.250.255
        inet6 fe80::42:7dff:fe38:10e2  prefixlen 64  scopeid 0x20<link>
        ether 02:42:7d:38:10:e2  txqueuelen 0  (Ethernet)
        RX packets 1  bytes 60 (60.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 11  bytes 836 (836.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

docker0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 172.17.0.1  netmask 255.255.0.0  broadcast 172.17.255.255
        ether 02:42:94:5c:c7:22  txqueuelen 0  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.0.0.81  netmask 255.0.0.0  broadcast 10.255.255.255
        inet6 fe80::20c:29ff:fe39:87f8  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:39:87:f8  txqueuelen 1000  (Ethernet)
        RX packets 4856  bytes 469922 (458.9 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 2898  bytes 441536 (431.1 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

eth1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.16.1.81  netmask 255.255.255.0  broadcast 172.16.1.255
        inet6 fe80::20c:29ff:fe39:8702  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:39:87:02  txqueuelen 1000  (Ethernet)
        RX packets 1  bytes 60 (60.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 11  bytes 836 (836.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

#标准输出重定向, 先清空,后写入, 如果文件不存在则创建

案例2:标准输出重定向(追加)

img

[root@web ~]# echo "111" >> abc
[root@web ~]# cat abc
br-c96a457c8696: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.250.1  netmask 255.255.255.0  broadcast 192.168.250.255
        inet6 fe80::42:7dff:fe38:10e2  prefixlen 64  scopeid 0x20<link>
        ether 02:42:7d:38:10:e2  txqueuelen 0  (Ethernet)
        RX packets 1  bytes 60 (60.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 11  bytes 836 (836.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

docker0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 172.17.0.1  netmask 255.255.0.0  broadcast 172.17.255.255
        ether 02:42:94:5c:c7:22  txqueuelen 0  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.0.0.81  netmask 255.0.0.0  broadcast 10.255.255.255
        inet6 fe80::20c:29ff:fe39:87f8  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:39:87:f8  txqueuelen 1000  (Ethernet)
        RX packets 4856  bytes 469922 (458.9 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 2898  bytes 441536 (431.1 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

eth1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.16.1.81  netmask 255.255.255.0  broadcast 172.16.1.255
        inet6 fe80::20c:29ff:fe39:8702  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:39:87:02  txqueuelen 1000  (Ethernet)
        RX packets 1  bytes 60 (60.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 11  bytes 836 (836.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

111

#标准追加输出重定向, 向配置文件末尾追加内容

案例3:错误输出重定向

img

[root@web ~]# useradd hg
[root@web ~]# su - hg

##//将标准输出和标准错误输出重定向到不同文件
[hg@web ~]$ find /etc/ -name "*.conf" 1>a 2>b

案例4:正确和错误都输入导相同位置

img

#方法1
#将标准输出和标准错误输出重定向到同一个文件, 混合输出
[hg@web ~]$ find /etc -name "*.conf" &>ab
[hg@web ~]$ find /etc -name "*.conf" >ab 2&>1

#合并两个文件内容至一个文件
[hg@web ~]$ cat a b > c

img

#方法2
#重定向到相同的位置
[root@web hg]# ls /root /error >ab  2>&1

img

# 空设备,即将产生的输出丢掉
[root@web hg]# ls /root /error >ab 2>/dev/null
[root@web hg]# ls /root /error >ab &>/dev/null

输入重定向

dd if=/dev/zero of=/file1.txt bs=1M count=20
dd if=/dev/zero of=/opt/disk bs=1K count=1024

dd </dev/zero >/opt/disk bs=1K count=1024