y !字符串 调用最后一个以该字符串开头的命令(常用)
9.3.3 输出重定向
--标准输入输出
键盘——/dev/stdin 文件描述符为0
显示器——/dev/sdtout 文件描述符为1
显示器——/dev/sdterr 文件描述符为2,类型是标准错误输出
背就背文件描述符就好了
--输出重定向
就是把原本应该标准输出的方向重新定向到文件(拐个弯)。
用处就是自动的记录等,有实际的用处,也常用。
比如
[root@andy ~]# ifconfig > test.log
[root@andy ~]# cat test.log
eth0 Link encap:Ethernet HWaddr 00:0C:29:61:EB:6B
inet addr:192.168.23.50 Bcast:192.168.23.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe61:eb6b/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:11069 errors:0 dropped:0 overruns:0 frame:0
TX packets:3521 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:798686 (779.9 KiB) TX bytes:447837 (437.3 KiB)
Interrupt:19 Base address:0x2000
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:20 errors:0 dropped:0 overruns:0 frame:0
TX packets:20 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:1532 (1.4 KiB) TX bytes:1532 (1.4 KiB)
命令的执行,可能会是定时用户来做的。
[root@andy ~]# ls > test.log
[root@andy ~]# cat test.log
anaconda-ks.cfg
cangls
hello.sh
install.log
install.log.syslog
japan
test.log
单个>,会覆盖结果;双>>是追加
[root@andy ~]# ifconfig >> test.log
[root@andy ~]# cat test.log
anaconda-ks.cfg
cangls
hello.sh
install.log
install.log.syslog
japan
test.log
eth0 Link encap:Ethernet HWaddr 00:0C:29:61:EB:6B
inet addr:192.168.23.50 Bcast:192.168.23.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe61:eb6b/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:11238 errors:0 dropped:0 overruns:0 frame:0
TX packets:3586 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:811445 (792.4 KiB) TX bytes:455539 (444.8 KiB)
Interrupt:19 Base address:0x2000
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:20 errors:0 dropped:0 overruns:0 frame:0
TX packets:20 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:1532 (1.4 KiB) TX bytes:1532 (1.4 KiB)
如果希望同时保存报错信息(犯2信息)
dateads 2>>test.log
另外注意,追加是有空格,错误输出是没有空格的2>>test.log
但是,最有用的是正确和错误都要
命令 >> 文件 2>&1
命令 &>> 文件 ——写到同一个文件
[root@andy ~]#
[root@andy ~]# ifconfig >> test2.log 2>&1
[root@andy ~]# cat test2.log
eth0 Link encap:Ethernet HWaddr 00:0C:29:61:EB:6B
inet addr:192.168.23.50 Bcast:192.168.23.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe61:eb6b/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:11444 errors:0 dropped:0 overruns:0 frame:0
TX packets:3638 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:825629 (806.2 KiB) TX bytes:461351 (450.5 KiB)
Interrupt:19 Base address:0x2000
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:20 errors:0 dropped:0 overruns:0 frame:0
TX packets:20 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:1532 (1.4 KiB) TX bytes:1532 (1.4 KiB)
[root@andy ~]# ifconfig2 >> test2.log 2>&1
[root@andy ~]# cat test2.log
eth0 Link encap:Ethernet HWaddr 00:0C:29:61:EB:6B
inet addr:192.168.23.50 Bcast:192.168