shell重定向
Linux操作系统默认把键盘作为标准输入,显示器或屏幕作为标准输出,以及标准错误输出。也就是从键盘输入命令,把返回信息和结果输出到显示器和屏幕上。
在实际当中,我们可以不使用系统默认的标准输入、标准输出,可以对他们重新设定,也就是重定向。重新定义输入、输出方式。
重定向操作符
1、输入重定向
输入重定向操作符经常使用的有“<”和“<<”,命令如下:
wc </etc/nscd.conf
通过wc命令,统计/etc/nscd.conf文件的行数、单词数量和字符数量。
重定向操作符“<<”,获取两个分隔号之间的内容,如:
[root@iZm5edmhc2bu5l6wo030p1Z ~]# wc << qq
将一对分隔号qq之间的内容,用作wc命令的输入内容,分隔号可以是任意字符,shell将读取两个分隔号之间的内容,本例以分隔号qq结尾,最后把结果交给wc处理。
2、输出重定向
输出重定向操作符有“>”和“>>”,命令如下:
[root@iZm5edmhc2bu5l6wo030p1Z ~]# ps -ef > 123.txt
把ps -ef的输出结果传送给123.txt,由于输出信息比较多,可以使用more命令查看123.txt的内容。
[root@iZm5edmhc2bu5l6wo030p1Z ~]# more 123.txt
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 Jan19 ? 00:00:01 /usr/lib/systemd/systemd --switched-root --system --deserialize 22
root 2 0 0 Jan19 ? 00:00:00 [kthreadd]
root 3 2 0 Jan19 ? 00:00:00 [ksoftirqd/0]
root 5 2 0 Jan19 ? 00:00:00 [kworker/0:0H]
root 6 2 0 Jan19 ? 00:00:00 [kworker/u4:0]
root 7 2 0 Jan19 ? 00:00:00 [migration/0]
root 8 2 0 Jan19 ? 00:00:00 [rcu_bh]
root 9 2 0 Jan19 ? 00:00:20 [rcu_sched]
root 10 2 0 Jan19 ? 00:00:00 [lru-add-drain]
root 11 2 0 Jan19 ? 00:00:00 [watchdog/0]
root 12 2 0 Jan19 ? 00:00:00 [watchdog/1]
root 13 2 0 Jan19 ? 00:00:00 [migration/1]
root 14 2 0 Jan19 ? 00:00:00 [ksoftirqd/1]
root 16 2 0 Jan19 ? 00:00:00 [kworker/1:0H]
root 18 2 0 Jan19 ? 00:00:00 [kdevtmpfs]
root 19 2 0 Jan19 ? 00:00:00 [netns]
root 20 2 0 Jan19 ? 00:00:00 [khungtaskd]
root 21 2 0 Jan19 ? 00:00:00 [writeback]
root 22 2 0 Jan19 ? 00:00:00 [kintegrityd]
root 23 2 0 Jan19 ? 00:00:00 [bioset]
root 24 2 0 Jan19 ? 00:00:00 [bioset]
root 25 2 0 Jan19 ? 00:00:00 [bioset]
root 26 2 0 Jan19 ? 00:00:00 [kblockd]
root 27 2 0 Jan19 ? 00:00:00 [md]
root 28 2 0 Jan19 ? 00:00:00 [edac-poller]
root 29 2 0 Jan19 ? 00:00:00 [watchdogd]
root 36 2 0 Jan19 ? 00:00:00 [kswapd0]
root 37 2 0 Jan19 ? 00:00:00 [ksmd]
root 38 2 0 Jan19 ? 00:00:00 [khugepaged]
root 39 2 0 Jan19 ? 00:00:00 [crypto]
root 47 2 0 Jan19 ? 00:00:00 [kthrotld]
root 48 2 0 Jan19 ? 00:00:00 [kworker/u4:1]
root 49 2 0 Jan19 ? 00:00:00 [kmpath_rdacd]
root 50 2 0 Jan19 ? 00:00:00 [kaluad]
root 51 2 0 Jan19 ? 00:00:00 [kpsmoused]
root 52 2 0 Jan19 ? 00:00:00 [ipv6_addrconf]
root 65 2 0 Jan19 ? 00:00:00 [deferwq]
root 96 2 0 Jan19 ? 00:00:00 [kauditd]
root 237 2 0 Jan19 ? 00:00:00 [ata_sff]
root 263 2 0 Jan19 ? 00:00:00 [scsi_eh_0]
root 266 2 0 Jan19 ? 00:00:00 [scsi_tmf_0]
root 267 2 0 Jan19 ? 00:00:00 [scsi_eh_1]
root 269 2 0 Jan19 ? 00:00:00 [scsi_tmf_1]
若“>”后面的文件不存在,系统会自动创建,如果文件存在,将会把原来文件的内容进行覆盖。如果需要保留原来文件的内容,就需要用到另外一个输出重定向命令“>>”,命令如下:
[root@iZm5edmhc2bu5l6wo030p1Z ~]# cat text.txt #查看text.txt文件的内容
#!/bin/bash
while :
do
clear
ifconfig eth0 |grep head
echo "当前网卡入口流量为: "
ifconfig eth0 |grep "RX p"
echo "当前网卡出口流量为: "
ifconfig eth0 |grep "TX p"
sleep 1
done
[root@iZm5edmhc2bu5l6wo030p1Z ~]# ls -l /etc/* >> text.txt #把/etc和子目录下的文件信息,添加到text.txt文件中,并保留原来文件的内容。
分屏查看text.txt文件的内容:
[root@iZm5edmhc2bu5l6wo030p1Z ~]# more text.txt
#!/bin/bash
while :
do
clear
ifconfig eth0 |grep head
echo "当前网卡入口流量为: "
ifconfig eth0 |grep "RX p"
echo "当前网卡出口流量为: "
ifconfig eth0 |grep "TX p"
sleep 1
done
-rw-r--r--. 1 root root 18 Jul 11 2019 /etc/adjtime
-rw-r--r--. 1 root root 1518 Jun 7 2013 /etc/aliases
-rw-r--r-- 1 root root 12288 Jul 11 2019 /etc/aliases.db
-rw------- 1 root root 541 Nov 20 2018 /etc/anacrontab
-rw-r--r--. 1 root root 55 Oct 30 2018 /etc/asound.conf
-rw-r--r-- 1 root root 1 Oct 31 2018 /etc/at.deny
-rw-r--r--. 1 root root 2853 Oct 31 2018 /etc/bashrc
-rw-r--r--. 1 root root 38 Dec 9 20:07 /etc/centos-release
-rw-r--r--. 1 root root 51 Nov 23 2018 /etc/centos-release-upstream
-rw-r--r--. 1 root root 1937 Dec 9 18:58 /etc/chrony.conf
-rw-r-----. 1 root chrony 481 Sep 15 2017 /etc/chrony.keys
-rw------- 1 root root 0 Nov 20 2018 /etc/cron.deny
-rw-r--r--. 1 root root 451 Jun 10 2014 /etc/crontab
-rw-------. 1 root root 0 Jul 11 2019 /etc/crypttab
-rw-r--r--. 1 root root 1620 Oct 31 2018 /etc/csh.cshrc
-rw-r--r--. 1 root root 866 Oct 31 2018 /etc/csh.login
-rw-r--r--. 1 root root 5090 Oct 30 2018 /etc/DIR_COLORS
-rw-r--r--. 1 root root 5725 Oct 30 2018 /etc/DIR_COLORS.256color
-rw-r--r--. 1 root root 4669 Oct 30 2018 /etc/DIR_COLORS.lightbgcolor
-rw-r--r--. 1 root root 1285 Nov 3 2018 /etc/dracut.conf
-rw-r--r-- 1 root root 112 Sep 30 21:21 /etc/e2fsck.conf
-rw-r--r--. 1 root root 0 Oct 31 2018 /etc/environment
-rw-r--r--. 1 root root 1317 Apr 11 2018 /etc/ethertypes
-rw-r--r--. 1 root root 0 Jun 7 2013 /etc/exports
lrwxrwxrwx. 1 root root 56 Jul 11 2019 /etc/favicon.png -> /usr/share/icons/hicolor/16x16/apps/fedora-logo-icon.png
-rw-r--r--. 1 root root 70 Oct 31 2018 /etc/filesystems
-rw-r--r-- 1 root root 313 Jul 11 2019 /etc/fstab
-rw-r--r-- 1 root root 38 Oct 30 2018 /etc/fuse.conf
-rw-r--r--. 1 root root 842 Oct 31 2018 /etc/GeoIP.conf
3、错误重定向
错误重定向操作符“2>“”和“2>>”,命令如下;
[root@iZm5edmhc2bu5l6wo030p1Z ~]# tar zxvf text.tar.gz 2> error.txt
如果text.tar.gz文件损坏,系统会把错误信息输出到error.txt文件内。