標(biāo)準(zhǔn)輸入、標(biāo)準(zhǔn)輸出、標(biāo)準(zhǔn)錯誤
輸出重定向及綜合案例
輸入重定向及結(jié)合案例
標(biāo)準(zhǔn)輸入、標(biāo)準(zhǔn)輸出、標(biāo)準(zhǔn)錯誤
file descriptors (FD,文件描述符 或 Process I/O channels):
進(jìn)程使用文件描述符來管理打開的文件
[root@CentOS7 ~]# ls /proc/$$/fd
0 1 2 3 4
0, 1, and 2, known as standard input, standard output, and standard error
輸出重定向 (覆蓋,追加)
正確輸出: 1> 1>> 等價于 > >>
錯誤輸出: 2> 2>>
案例1:輸出重定向(覆蓋)
[root@CentOS7 ~]# date 1> date.txt
案例2:輸出重定向(追加)
[root@CentOS7 ~]# date >> date.txt
案例3:錯誤輸出重定向
[root@CentOS7 ~]# ls /home/ /aaaaaaaaa >list.txt
ls: 無法訪問/aaaaaaaaa: 沒有那個文件或目錄
[root@CentOS7 ~]# ls /home/ /aaaaaaaaa >list.txt 2>error.txt //重定向到不同的位置
案例4: 正確和錯誤都輸入到相同位置
[root@CentOS7 ~]# ls /home/ /aaaaaaaaa &>list.txt //混合輸出
案例5: 正確和錯誤都輸入到相同位置
[root@CentOS7 ~]# ls /home/ /aaaaaaaaa >list.txt 2>&1 //重定向到相同的位置
案例6:重定向到空設(shè)備/dev/null
[root@CentOS7 ~]# ls /home/ /aaaaaaaaa >list.txt 2>/dev/null //空設(shè)備,即將產(chǎn)生的輸出丟掉
[root@CentOS7 ~]# ls /home/ /aaaaaaaaa &>/dev/null //空設(shè)備,即將產(chǎn)生的輸出丟掉
cp /etc/passwd /dev/null ???
cp /etc/passwd /etc/passwd1 2>/dev/null ???
sudo rm /dev/null
sudo mknod -m 666 /dev/null c 1 3
案例7:腳本中使用重定向
# vim ping.sh
#!/usr/bin/bash
ping -c1 172.16.120.254 &>/dev/null
if [ $? -eq 0 ];then
echo "up.."
else
echo "down.."
fi
# bash ping.sh
案例8:腳本中使用重定向
# vim ping2.sh
#!/usr/bin/bash
ping -c1 172.16.120.254 &>/dev/null
if [ $? -eq 0 ];then
echo "172.16.120.254 up.." > /up.txt
else
echo "172.16.120.254 down.." >/down.txt
fi
# bash ping2.sh
>new.txt ???
>/etc/passwd ???
>/etc ???
輸入重定向
標(biāo)準(zhǔn)輸入: < 等價 0<
案例1:
[root@CentOS7 ~]# mail -s "ssss" alice //沒有改變輸入的方向,默認(rèn)鍵盤
111
222
333
^D
[root@CentOS7 ~]# su - alice
[alice@CentOS7 ~]$ mail
Mail version 8.1 6/6/93. Type ? for help.
"/var/spool/mail/alice": 1 message 1 new
>N 1 root@CentOS7.local Mon Oct 29 14:09 18/657 "ssss"
&
[root@CentOS7 ~]# mail -s "test01" alice < /etc/hosts //輸入重定向,來自于文件
案例2:
[root@CentOS7 ~]# grep 'root' //沒有改變輸入的方向,默認(rèn)鍵盤,此時等待輸入...
yang sss
sssrootssss..
sssrootssss..
[root@CentOS7 ~]# grep 'root' < /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
案例3:
[root@CentOS7 ~]# dd if=/dev/zero of=/file1.txt bs=1M count=2
[root@CentOS7 ~]# dd </dev/zero >/file2.txt bs=1M count=20
案例4:mysql表結(jié)構(gòu)導(dǎo)入
[root@CentOS7 ~]# mysql -uroot -p123 < bbs.sql
案例5:at
[root@CentOS7 ~]# at now +5 min
at> useradd yang99
at> <EOT>
job 1 at 2015-06-09 11:57
[root@CentOS7 ~]# vim at.txt
sudo useradd yang100
sudo useradd yang102
[root@CentOS7 ~]# at now +2 min < at.txt
job 2 at 2015-06-09 11:55
sudo: sorry, you must have a tty to run sudo
sudo: sorry, you must have a tty to run sudo
綜合案例1: 利用重定向建立多行的文件
[root@CentOS7 ~]# echo "111" > file1.txt
[root@CentOS7 ~]# cat file1.txt
111
[root@CentOS7 ~]# cat >file2.txt
111
222
333
444
^D
[root@CentOS7 ~]# cat file2.txt
請問:file2.txt有幾行?
[root@CentOS7 ~]# cat >>file3.txt
aaa
bbb
ccc
ddd
^D
[root@CentOS7 ~]# cat file3.txt
請問:file3.txt有幾行?
[root@CentOS7 ~]# cat >file4 <<EOF
> 111
> 222
> 333
> EOF
[root@CentOS7 ~]# cat file4
111
222
333
綜合案例2: 腳本中利用重定向打印消息
[root@CentOS7 ~]# vim yang.sh
#!/usr/bin/bash
cat <<-EOF
+------------------------------------------------+
| |
| ====================== |
| 虛擬機(jī)基本管理centos |
| by CentOS7 |
| ====================== |
| 1. 安裝虛擬機(jī) |
| 2. 重置所有Linux虛擬機(jī) |
| 3. 重置Windows虛擬機(jī) |
| 4. 重置Windows虛擬機(jī) [完全] |
| 5. 重置指定的虛擬機(jī) |
| q. 退出管理程序 |
| |
+------------------------------------------------+
EOF
綜合案例3
[root@CentOS7 ~]# ls; date &>/dev/null
[root@CentOS7 ~]# ls &>/dev/null; date &>/dev/null
[root@CentOS7 ~]# (ls; date) &>/dev/null
[root@CentOS7 ~]# (while :; do date; sleep 2; done) &>date.txt
[root@CentOS7 ~]# (while :; do date; sleep 2; done) &>date.txt &
[1] 6595
[root@CentOS7 ~]# tailf date.txt
Tue Apr 12 22:04:32 CST 2016
Tue Apr 12 22:04:34 CST 2016
Tue Apr 12 22:04:36 CST 2016
Tue Apr 12 22:04:38 CST 2016
Tue Apr 12 22:04:40 CST 2016
Tue Apr 12 22:04:42 CST 2016
Tue Apr 12 22:04:44 CST 2016
Tue Apr 12 22:04:46 CST 2016
Tue Apr 12 22:04:48 CST 2016
[root@CentOS7 ~]# jobs
[1]+ Running ( while :; do
date; sleep 2;
done ) &>date.txt &
[root@CentOS7 ~]# kill %1
[root@CentOS7 ~]# jobs
[root@CentOS7 ~]# (./configure && make && make install) &>/dev/null
擴(kuò)展點(diǎn):subshell
[root@CentOS7 ~]# (umask 777; touch file8888)
[root@CentOS7 ~]#
[root@CentOS7 ~]# ll file8888
---------- 1 root root 0 Apr 12 22:11 file8888
[root@CentOS7 ~]#
[root@CentOS7 ~]# umask
0022
· Use redirection characters to control output to files.
· Use piping to control output to other programs.
files: > 2> file1.txt /dev/pts/2 /dev/tty1 /dev/null /dev/sda
programs: |
進(jìn)程管道
用法:command1 | command2 |command3 |...
[root@CentOS7 ~]# ll /dev/ |less
[root@CentOS7 ~]# ps aux |grep 'sshd'
[root@CentOS7 ~]# rpm -qa |grep 'httpd' //查詢所有安裝的軟件包,過濾包含httpd的包
[root@CentOS7 ~]# yum list |grep 'httpd'
案例1:將/etc/passwd中的用戶按UID大小排序
[root@CentOS7 ~]# sort -t":" -k3 -n /etc/passwd //以: 分隔,將第三列按字?jǐn)?shù)升序
[root@CentOS7 ~]# sort -t":" -k3 -n /etc/passwd -r //逆序
[root@CentOS7 ~]# sort -t":" -k3 -n /etc/passwd |head
-t 指定字段分隔符--field-separator
-k 指定列
-n 按數(shù)值
案例2:統(tǒng)計出最占CPU的5個進(jìn)程
[root@CentOS7 ~]# ps aux --sort=-%cpu |head -6
案例3:統(tǒng)計當(dāng)前/etc/passwd中用戶使用的shell類型
思路:取出第七列(shell) | 排序(把相同歸類)| 去重
[root@CentOS7 ~]# awk -F: '{print $7}' /etc/passwd
[root@CentOS7 ~]# awk -F: '{print $7}' /etc/passwd |sort
[root@CentOS7 ~]# awk -F: '{print $7}' /etc/passwd |sort |uniq
[root@CentOS7 ~]# awk -F: '{print $7}' /etc/passwd |sort |uniq -c
131 /bin/bash
1 /bin/sync
1 /sbin/halt
63 /sbin/nologin
1 /sbin/shutdown
-F: 指定字段分隔符
$7 第七個字段
案例4: 統(tǒng)計網(wǎng)站的訪問情況 top 20
思路: 打印所有訪問的連接 | 過濾訪問網(wǎng)站的連接 | 打印用戶的IP | 排序 | 去重
[root@CentOS7 ~]# ss -an |grep :80 |awk -F":" '{print $8}' |sort |uniq -c
4334 192.168.0.66
1338 192.168.10.11
1482 192.168.10.125
44 192.168.10.183
3035 192.168.10.213
375 192.168.10.35
362 192.168.10.39
[root@CentOS7 ~]# ss -an |grep :80 |awk -F":" '{print $8}' |sort |uniq -c |sort -k1 -rn |head -n 20
案例5: 打印當(dāng)前所有IP
[root@CentOS7 ~]# ip addr |grep 'inet ' |awk '{print $2}' |awk -F"/" '{print $1}'
127.0.0.1
192.168.2.115
案例6:打印根分區(qū)已用空間的百分比(僅打印數(shù)字)
[root@CentOS7 ~]# df -P |grep '/$' |awk '{print $5}' |awk -F"%" '{print $1}'
tee管道
[root@CentOS7 ~]# ip addr |grep 'inet ' |tee ip.txt |awk -F"/" '{print $1}' |awk '{print $2}'
127.0.0.1
172.16.60.1
[root@CentOS7 ~]# cat ip.txt
inet 127.0.0.1/8 scope host lo
inet 172.16.60.1/24 brd 172.16.60.255 scope global eth0
[root@CentOS7 ~]# ip addr |grep 'inet ' |tee -a ip.txt |awk -F"/" '{print $1}' |awk '{print $2}'
127.0.0.1
172.16.60.1
[root@CentOS7 ~]# date |tee date.txt
Sat Mar 11 10:22:31 CST 2017
awk sed grep sort uniq less more xargs
xargs: ls cp rm
案例1
[root@localhost ~]# touch /home/file{1..5}
[root@localhost ~]# vim files.txt
/home/file1
/home/file2
/home/file3
/home/file4
/home/file5
[root@localhost ~]# cat files.txt |ls -l
[root@localhost ~]# cat files.txt |rm -rvf
cont.
[root@localhost ~]# cat files.txt |xargs ls -l
-rw-r--r--. 1 root root 0 Mar 11 10:35 /home/file1
-rw-r--r--. 1 root root 0 Mar 11 10:35 /home/file2
-rw-r--r--. 1 root root 0 Mar 11 10:35 /home/file4
-rw-r--r--. 1 root root 0 Mar 11 10:35 /home/file5
[root@localhost ~]# cat files.txt |xargs rm -rvf
removed '/home/file1’
removed '/home/file2’
removed '/home/file4’
removed '/home/file5’
案例2
[root@localhost ~]# touch /home/file{1..5}
[root@localhost ~]# cat files.txt |xargs -I {} ls -l {}
-rw-r--r--. 1 root root 0 Mar 11 10:40 /home/file1
-rw-r--r--. 1 root root 0 Mar 11 10:40 /home/file2
-rw-r--r--. 1 root root 0 Mar 11 10:40 /home/file4
-rw-r--r--. 1 root root 0 Mar 11 10:40 /home/file5
[root@localhost ~]# cat files.txt |xargs -I {} cp -rvf {} /tmp
'/home/file1’ -> '/tmp/file1’
'/home/file2’ -> '/tmp/file2’
'/home/file4’ -> '/tmp/file4’
'/home/file5’ -> '/tmp/file5’
[root@localhost ~]# cat files.txt |xargs -I YANG cp -rvf YANG /var/tmp
'/home/file1’ -> '/var/tmp/file1’
'/home/file2’ -> '/var/tmp/file2’
'/home/file4’ -> '/var/tmp/file4’
'/home/file5’ -> '/var/tmp/file5’
案例3
[root@localhost ~]# find /etc -iname "*ifcfg*" |xargs -I {} cp -rf {} /tmp
聯(lián)系客服