重定向
什么是重定向:
## 标准的输入设备默认指的是键盘,标准的输出设备默认指的是显示器。#- 输入重定向:指定设备(通常是文件或命令的执行结果)来代替键盘作为新的输入设备;#- 输出重定向:将原本要输出在屏幕上的结果放到指定的设备(通常是文件)## 标准的输入设备默认指的是键盘,标准的输出设备默认指的是显示器。 #- 输入重定向:指定设备(通常是文件或命令的执行结果)来代替键盘作为新的输入设备; #- 输出重定向:将原本要输出在屏幕上的结果放到指定的设备(通常是文件)## 标准的输入设备默认指的是键盘,标准的输出设备默认指的是显示器。 #- 输入重定向:指定设备(通常是文件或命令的执行结果)来代替键盘作为新的输入设备; #- 输出重定向:将原本要输出在屏幕上的结果放到指定的设备(通常是文件)
为何用(重定向的作用)
1.当屏幕输出的信息很重要,而且希望保存重要的信息备份2.不干扰屏幕的使用 ping baidu3.储存错误/报错日志4.定时任务的运行结果5.执行一个命令,把报错信息丢弃到/dev/null5.执行一个命令,可能报错和正确的输出并存,类似错误日志与标准正确日志需要分别输出至不同的文件。1.当屏幕输出的信息很重要,而且希望保存重要的信息备份 2.不干扰屏幕的使用 ping baidu 3.储存错误/报错日志 4.定时任务的运行结果 5.执行一个命令,把报错信息丢弃到/dev/null 5.执行一个命令,可能报错和正确的输出并存,类似错误日志与标准正确日志需要分别输出至不同的文件。1.当屏幕输出的信息很重要,而且希望保存重要的信息备份 2.不干扰屏幕的使用 ping baidu 3.储存错误/报错日志 4.定时任务的运行结果 5.执行一个命令,把报错信息丢弃到/dev/null 5.执行一个命令,可能报错和正确的输出并存,类似错误日志与标准正确日志需要分别输出至不同的文件。
输入和输出
执行一个shell程序时通常会自动打开三个文件描述符
名称 | 文件描述符 | |
标准输入(stdin) | 0 | 键盘,也可以是其他文件或者命令的输出的内容可以作为标准输入 |
标准输出(stdout) | 1 | 默认输出到屏幕 |
错误输出(stderr) | 2 | 默认输出到屏幕 |
文件名称(filename) | 3+ |
进程将从标准输入中得到数据,将正常输出打印至屏幕终端,将错误的输出信息也打印至屏幕终端。
进程使用文件描述符(file descriptors)来管理打开的文件
![Linux系统管理:输入输出重定向 图片[1]-Linux系统管理:输入输出重定向-不念博客](https://www.bunian.cn/wp-content/uploads/2023/01/bedfd1dc0a0a4834970e4c01fef83269_noop.jpeg)
以cat命令为例, cat命令的功能是从命令行给出的文件中读取数据,并将这些数据直接送到标准输出。
直接用cat:# cat /etc/passwd会把/etc/passwd的内容依次显示到屏幕上# cat cat命令行中没有参数, 它就会从标准输入中读取数据, 并将其送到标准输出。hello 按enter后用户输入的每一行都立刻被cat命令输出到屏幕上。hello直接用cat: # cat /etc/passwd 会把/etc/passwd的内容依次显示到屏幕上 # cat cat命令行中没有参数, 它就会从标准输入中读取数据, 并将其送到标准输出。 hello 按enter后用户输入的每一行都立刻被cat命令输出到屏幕上。 hello直接用cat: # cat /etc/passwd 会把/etc/passwd的内容依次显示到屏幕上 # cat cat命令行中没有参数, 它就会从标准输入中读取数据, 并将其送到标准输出。 hello 按enter后用户输入的每一行都立刻被cat命令输出到屏幕上。 hello
输出重定向
符号格式 | 作用 |
命令> /1>文件 | 标准输出重定向(覆盖) 将命令执行的结果放到文件中,不显示在屏幕上,覆盖文件内容 |
命令>>文件 | 标准输出重定向(追加) 将命令执行的结果输出到指定的文件中,不会覆盖,如果该文件已包含数据,新数据将写入到原有内容的后面。 |
命令2>文件 | 错误输出重定向(覆盖) 将原本要显示在屏幕的报错内容放入指定文件,会先清空文件,再覆盖原有内容 |
命令2>>文件 | 错误输出重定向(追加) 将原本要显示在屏幕的报错内容追加到指定文件 |
例1 标准输出重定向(覆盖)
[root@localhost ~]# ifconfig eth0 > abc (会自动创建文件abc)[root@localhost ~]# cat abceth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500inet 10.0.0.251 netmask 255.255.255.0 broadcast 10.0.0.255inet6 fe80::9bc0:f459:3a67:bd7a prefixlen 64 scopeid 0x20<link>inet6 fe80::152f:f2c7:21b3:412b prefixlen 64 scopeid 0x20<link>inet6 fe80::e1af:89d9:4338:31df prefixlen 64 scopeid 0x20<link>ether 00:0c:29:c3:b0:5d txqueuelen 1000 (Ethernet)RX packets 2812 bytes 244635 (238.9 KiB)RX errors 0 dropped 0 overruns 0 frame 0TX packets 1879 bytes 363592 (355.0 KiB)TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0[root@localhost ~]# ifconfig eth0 > abc (会自动创建文件abc) [root@localhost ~]# cat abc eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 10.0.0.251 netmask 255.255.255.0 broadcast 10.0.0.255 inet6 fe80::9bc0:f459:3a67:bd7a prefixlen 64 scopeid 0x20<link> inet6 fe80::152f:f2c7:21b3:412b prefixlen 64 scopeid 0x20<link> inet6 fe80::e1af:89d9:4338:31df prefixlen 64 scopeid 0x20<link> ether 00:0c:29:c3:b0:5d txqueuelen 1000 (Ethernet) RX packets 2812 bytes 244635 (238.9 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 1879 bytes 363592 (355.0 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0[root@localhost ~]# ifconfig eth0 > abc (会自动创建文件abc) [root@localhost ~]# cat abc eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 10.0.0.251 netmask 255.255.255.0 broadcast 10.0.0.255 inet6 fe80::9bc0:f459:3a67:bd7a prefixlen 64 scopeid 0x20<link> inet6 fe80::152f:f2c7:21b3:412b prefixlen 64 scopeid 0x20<link> inet6 fe80::e1af:89d9:4338:31df prefixlen 64 scopeid 0x20<link> ether 00:0c:29:c3:b0:5d txqueuelen 1000 (Ethernet) RX packets 2812 bytes 244635 (238.9 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 1879 bytes 363592 (355.0 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
例2 标准输出重定向(追加)
[root@localhost ~]# echo "This is network conf" >> if (没有自动创建了if文件)[root@localhost ~]# cat ifThis is network conf[root@localhost ~]# echo 111 >> if[root@localhost ~]# cat ifThis is network conf111[root@localhost ~]# echo "This is network conf" >> if (没有自动创建了if文件) [root@localhost ~]# cat if This is network conf [root@localhost ~]# echo 111 >> if [root@localhost ~]# cat if This is network conf 111[root@localhost ~]# echo "This is network conf" >> if (没有自动创建了if文件) [root@localhost ~]# cat if This is network conf [root@localhost ~]# echo 111 >> if [root@localhost ~]# cat if This is network conf 111
例3 错误输出重定向
[root@localhost ~]# su - xxx[xxx@localhost ~]$ find /etc -name "*.conf" (标准输出和错误全部显示在屏幕上)[xxx@localhost ~]$ find /etc -name "*.conf" 1>a 2>b (标准输出进文件a,错误进b)[xxx@localhost ~]$ cat bfind: ‘/etc/grub.d’: Permission deniedfind: ‘/etc/polkit-1/rules.d’: Permission deniedfind: ‘/etc/polkit-1/localauthority’: Permission deniedfind: ‘/etc/pki/CA/private’: Permission deniedfind: ‘/etc/pki/rsyslog’: Permission denied......(省略)&符[zls@zls ~]$ find /etc -name "*.conf" &>ab (全进文件ab)[root@localhost ~]# crontab -e 编辑定时任务# 在里面输入:* * * * * /usr/sbin/ntpdate time1.aliyun.com &>/dev/null[root@localhost ~]# su - xxx [xxx@localhost ~]$ find /etc -name "*.conf" (标准输出和错误全部显示在屏幕上) [xxx@localhost ~]$ find /etc -name "*.conf" 1>a 2>b (标准输出进文件a,错误进b) [xxx@localhost ~]$ cat b find: ‘/etc/grub.d’: Permission denied find: ‘/etc/polkit-1/rules.d’: Permission denied find: ‘/etc/polkit-1/localauthority’: Permission denied find: ‘/etc/pki/CA/private’: Permission denied find: ‘/etc/pki/rsyslog’: Permission denied ......(省略) &符 [zls@zls ~]$ find /etc -name "*.conf" &>ab (全进文件ab) [root@localhost ~]# crontab -e 编辑定时任务 # 在里面输入: * * * * * /usr/sbin/ntpdate time1.aliyun.com &>/dev/null[root@localhost ~]# su - xxx [xxx@localhost ~]$ find /etc -name "*.conf" (标准输出和错误全部显示在屏幕上) [xxx@localhost ~]$ find /etc -name "*.conf" 1>a 2>b (标准输出进文件a,错误进b) [xxx@localhost ~]$ cat b find: ‘/etc/grub.d’: Permission denied find: ‘/etc/polkit-1/rules.d’: Permission denied find: ‘/etc/polkit-1/localauthority’: Permission denied find: ‘/etc/pki/CA/private’: Permission denied find: ‘/etc/pki/rsyslog’: Permission denied ......(省略) &符 [zls@zls ~]$ find /etc -name "*.conf" &>ab (全进文件ab) [root@localhost ~]# crontab -e 编辑定时任务 # 在里面输入: * * * * * /usr/sbin/ntpdate time1.aliyun.com &>/dev/null
输入重定向
输入重定向,即原本从键盘等设备上获得的输入信息,重定向由命令的输出作为输入。
命令 < 文件 将指定文件作为命令的输入设备命令 << 分界符 表示从标准输入设备(键盘)中读入,直到遇到分界符才停止(读入的数据不包括分界符)命令 < 文件1 > 文件2 将文件1作为命令的输入设备,该命令的执行结果输出到文件2 中。命令 < 文件 将指定文件作为命令的输入设备 命令 << 分界符 表示从标准输入设备(键盘)中读入,直到遇到分界符才停止(读入的数据不包括分界符) 命令 < 文件1 > 文件2 将文件1作为命令的输入设备,该命令的执行结果输出到文件2 中。命令 < 文件 将指定文件作为命令的输入设备 命令 << 分界符 表示从标准输入设备(键盘)中读入,直到遇到分界符才停止(读入的数据不包括分界符) 命令 < 文件1 > 文件2 将文件1作为命令的输入设备,该命令的执行结果输出到文件2 中。
例1
[root@localhost ~]# cat /etc/passwd[root@localhost ~]# cat < /etc/passwd 输出结果同上面命令相同[root@localhost ~]# grep 'root' 等待输入cchkjnj[root@localhost ~]# grep 'root' < /etc/passwd 输入设备变为/etc/passwdroot:x:0:0:root:/root:/bin/bash[root@localhost ~]# cat /etc/passwd [root@localhost ~]# cat < /etc/passwd 输出结果同上面命令相同 [root@localhost ~]# grep 'root' 等待输入 cch kjnj [root@localhost ~]# grep 'root' < /etc/passwd 输入设备变为/etc/passwd root:x:0:0:root:/root:/bin/bash[root@localhost ~]# cat /etc/passwd [root@localhost ~]# cat < /etc/passwd 输出结果同上面命令相同 [root@localhost ~]# grep 'root' 等待输入 cch kjnj [root@localhost ~]# grep 'root' < /etc/passwd 输入设备变为/etc/passwd root:x:0:0:root:/root:/bin/bash
例2
[root@localhost ~]# cat a.txt[root@localhost ~]# cat < /etc/passwd > a.txt[root@localhost ~]# cat a.txt 输出了和 /etc/passwd 文件内容相同的数据[root@localhost ~]# cat a.txt [root@localhost ~]# cat < /etc/passwd > a.txt [root@localhost ~]# cat a.txt 输出了和 /etc/passwd 文件内容相同的数据[root@localhost ~]# cat a.txt [root@localhost ~]# cat < /etc/passwd > a.txt [root@localhost ~]# cat a.txt 输出了和 /etc/passwd 文件内容相同的数据
自己操作试验:
[root@localhost ~]# vim 2.txt 新建一个2.txt 输入:“不管是昨天的风”[root@localhost ~]# cat > 2.txt 需要按ctrl D结束输入woshii^Hnnibabababab[root@localhost ~]# cat 2.txt 发现被覆盖了woshinnibabababab[root@localhost ~]# cat > 2.txt < A 按A结束输入 一个<不行bash: A: No such file or directory[root@localhost ~]# cat > 2.txt << A> hahahahah> A[root@localhost ~]# cat >> 2.txt << A >>追加,不覆盖> hohouhhh> A[root@localhost ~]# cat 2.txthahahahahhohouhhh[root@localhost ~]# vim 2.txt 新建一个2.txt 输入:“不管是昨天的风” [root@localhost ~]# cat > 2.txt 需要按ctrl D结束输入 woshii^Hnnibaba babab [root@localhost ~]# cat 2.txt 发现被覆盖了 woshinnibaba babab [root@localhost ~]# cat > 2.txt < A 按A结束输入 一个<不行 bash: A: No such file or directory [root@localhost ~]# cat > 2.txt << A > hahahahah > A [root@localhost ~]# cat >> 2.txt << A >>追加,不覆盖 > hohouhhh > A [root@localhost ~]# cat 2.txt hahahahah hohouhhh[root@localhost ~]# vim 2.txt 新建一个2.txt 输入:“不管是昨天的风” [root@localhost ~]# cat > 2.txt 需要按ctrl D结束输入 woshii^Hnnibaba babab [root@localhost ~]# cat 2.txt 发现被覆盖了 woshinnibaba babab [root@localhost ~]# cat > 2.txt < A 按A结束输入 一个<不行 bash: A: No such file or directory [root@localhost ~]# cat > 2.txt << A > hahahahah > A [root@localhost ~]# cat >> 2.txt << A >>追加,不覆盖 > hohouhhh > A [root@localhost ~]# cat 2.txt hahahahah hohouhhh
© 版权声明
本站文章由不念博客原创,未经允许严禁转载!
THE END