一、重定向概述
1.1、什么是重定向?
將原本要輸出到屏幕的數據信息,重新定向到指定的文件中。
比如:每天凌晨定時備份數據,希望將備份數據的結果保存到某個文件中。這樣第二天通過查看結果文件,就知道昨天備份數據是成功還是失敗。
1.2、為何要使用重定向?
1)當屏幕輸出的信息很重要,而且希望保存重要的信息的時候。
2)后台執行中的程序,不希望它干擾屏幕的正常輸出結果的時候。
3)系統的例行命令,比如定時任務的執行結果,希望它可以存下來的時候。
4)一些執行命令,我們已經知道它可能出現錯誤信息,想將它直接丟棄的時候。
5)執行一個命令,可能報錯輸出和正確輸出並存,類似錯誤日志與正確日志需要分別輸出到不同的文件的時候。
1.3、學習重定向的預備知識,標准輸入與輸出
當運行一個shell
程序時通常會自動打開三個標准文件,分別是標准輸入、標准輸出、錯誤輸出。
1.4、標准輸入與輸出
執行一個shell程序時通常會自動打開三個文件描述符
名稱 | 文件描述符 | 作用 |
---|---|---|
標准輸入(stdin) | 0 | 通常是鍵盤,也可以是其他文件或者命令的輸出。 |
標准輸出(stdout) | 1 | 默認輸出到屏幕。 |
錯誤輸出(stderr) | 2 | 默認輸出到屏幕。 |
文件名稱(filename) | 3+ |
進程將從標准輸入中得到數據,將正常輸出打印至屏幕終端,將錯誤的輸出信息也打印至屏幕終端。進程是使用文件描述符(file descriptors)
來管理打開的文件。
以cat
命令為例,cat
命令的功能是從命令行給出的文件讀取數據,並將這些數據直接送到標准輸出。
#若使用如下命令,會把文件/etc/passwd的內容依次顯示到屏幕上
[root@centos7 ~]# cat /etc/passwd
但如果使用cat
命令沒有更上輸入的文件名,那么cat
命令則會通過命令行標准輸入中讀取數據,並將其送到標准輸出。
#用戶輸入的每一行都立刻被cat命令輸出到屏幕上。
[root@centos7 ~]# cat
hello #標准輸入
hello #標准輸出
^c
下面了解一下標准輸入輸出過程
#持續追蹤查看文件內容
[root@centos7 ~]# tail -f /etc/passwd
ctrl+z 將進程轉到后台
#查看運行的進程
[root@centos7 ~]# ps
PID TTY TIME CMD
5848 pts/1 00:00:00 bash
6885 pts/1 00:00:00 tail
6888 pts/1 00:00:00 ps
#查看tail命令的pid,6885進程下的文件描述符
[root@centos7 ~]# ls -l /proc/6885/fd
total 0
lrwx------ 1 root root 64 Dec 3 06:57 0 -> /dev/pts/1
lrwx------ 1 root root 64 Dec 3 06:57 1 -> /dev/pts/1
lrwx------ 1 root root 64 Dec 3 06:56 2 -> /dev/pts/1
lr-x------ 1 root root 64 Dec 3 06:57 3 -> /etc/passwd
lr-x------ 1 root root 64 Dec 3 06:57 4 -> inotify
#Linux查看標准輸入輸出設備
[root@xuliangwei ~]# ls -l /dev/std*
lrwxrwxrwx 1 root root 15 Dec 2 22:30 /dev/stderr -> /proc/self/fd/2
lrwxrwxrwx 1 root root 15 Dec 2 22:30 /dev/stdin -> /proc/self/fd/0
lrwxrwxrwx 1 root root 15 Dec 2 22:30 /dev/stdout -> /proc/self/fd/1
二、輸出重定向
重定向:改變標准輸入、標准輸出的方向就是重定向。
類型 | 操作符 | 用途 |
---|---|---|
標准覆蓋輸出重定向 | > | 將程序輸出的正確結果輸出到指定的文件中,會覆蓋文件原有的內容 |
標准追加輸出重定向 | 將程序輸出的正確結果以追加的方式輸出到指定文件,不會覆蓋原有文件 | |
錯誤覆蓋輸出重定向 | 2> | 將程序的錯誤結果輸出到執行的文件中,會覆蓋文件原有的內容 |
錯誤追加輸出重定向 | 2>> | 將程序輸出的錯誤結果以追加的方式輸出到指定文件,不會覆蓋原有文件 |
標准輸入重定向 | << | 將命令中接收輸入的途徑由默認的鍵盤更改為指定的文件或命令 |
示例1:標准輸出重定向(每次都會覆蓋文件)
#標准輸出重定向, 先清空,后寫入, 如果文件不存在則創建
[root@centos7 ~]# ifconfig ens33 > abc

示例2:標准追加輸出重定向(會向文件的尾部添加內容)
#標准追加輸出重定向,向配置文件末尾追加內容
[root@centos7 ~]# ifconfig ens33 >> abc
[root@centos7 ~]# echo "This is network conf" >> if

示例3:錯誤輸出重定向
#正確輸出以及錯誤輸出重定向至一個文件
[root@centos7 ~]# useradd file_1
[root@centos7 ~]# su - file_1
#將標准輸出和標准錯誤輸出重定向到不同文件
[root@centos7 ~]# find /etc -name "*.conf" 1>a 2>b

示例4:正確和錯誤都輸入到相同位置
#將標准輸出和標准錯誤輸出重定向到同一個文件, 混合輸出
[root@centos7 ~]# find /etc -name "*.conf" &>ab
#合並兩個文件內容至一個文件
[root@centos7 ~]# cat a b > c

示例5:正確和錯誤都輸入到相同位置
#重定向到相同的位置
[root@centos7 ~]# ls /root /error >ab 2>&1

示例6:重定向到空設備/dev/null
#將產生的任何數據放入黑洞設備,則視為丟棄。
[root@xuliangwei ~]# ls /root /error >ab 2>/dev/null
[root@xuliangwei ~]# ls /root /error >ab &>/dev/null
#將錯誤輸出重定向到 ‘黑洞’,正確內容輸出到屏幕/dev/pts/x
[root@centos7 ~]# ls /root/ /err 2>/dev/null
#將錯誤輸出重定向到 ‘黑洞’,正確內容輸出到1.txt文件中
[root@centos7 ~]# ls /root/ /err >1.txt 2>/dev/null

示例6:腳本中使用重定向
[root@centos7 ~]# vim ping.sh
#!/bin/bash
. /etc/init.d/functions
read -p "請輸入要檢測的IP:" IP
ping -c1 -W1 $IP >/dev/null
if [ $? -eq 0 ];then
action "$IP" /bin/true >> /tmp/IP_OK.txt
else
action "$IP" /bin/false>> /tmp/IP_FAILD.txt
fi
[root@zls ~]# sh ping.sh
#升級版:
用for循環檢測255個:
#!/bin/bash
. /etc/init.d/functions
for n in `seq 255`;do
ping -c1 -W1 "10.0.0.$n" &>/dev/null
if [ $? -eq 0 ];then
action "10.0.0.$n" /bin/true
else
action "10.0.0.$n" /bin/false
fi
done
三、輸入的重定向使用
輸入重定向,即原本從鍵盤等設備上獲得的輸入信息,重定向由命令的輸出作為輸入。
標准輸入: < 等價於 0<
<< 等價於 0<<
示例1:從文件中讀入輸入的操作
#沒有改變輸入的方向,默認鍵盤
[root@centos7 ~]# mail zls
Subject: hello
1111
2222
3333
. #結束
EOT
#檢查是否收到郵件
[root@centos7 ~]# su - zls
[root@centos7 ~]# mail
//輸入重定向,來自於文件
[root@centos7 ~]# mail -s "test01" zls < /etc/hosts
示例2:
#沒有改變輸入的方向,默認鍵盤,此時等待輸入
[root@centos7 ~]# grep 'root'
xxx
xxx
[root@centos7 ~]# grep 'root' < /etc/passwd
root:x:0:0:root:/root:/bin/bash
示例3:
[root@centos7 ~]# dd if=/dev/zero of=/file1.txt bs=1M count=20
20+0 records in
20+0 records out
20971520 bytes (21 MB) copied, 0.0260574 s, 805 MB/s
[root@centos7 ~]# dd </dev/zero >/file2.txt bs=1M count=20
20+0 records in
20+0 records out
20971520 bytes (21 MB) copied, 0.011896 s, 1.8 GB/s
示例4:mysql如何恢復備份,了解即可,不用關注。
#mysql 導入數據
[root@centos7 ~]# mysql -uroot -p123 < bbs.sql
示例5:利用重定向建立多行文件
#手動執行 shell 命令
[root@xuliangwei ~]# echo "111" > file1.txt
[root@xuliangwei ~]# cat file1.txt
111
[root@xuliangwei ~]# cat >file2.txt
111
222
333
^D
[root@xuliangwei ~]# cat >>file3.txt
aaa
bbb
ccc
^D
#腳本 script 創建多行文件
[root@centos7 ~]# vim create_file.sh
cat >file200.txt <<EOF
111
222
333
yyy
ccc
EOF
#腳本中打印菜單的一種使用方法
[root@centos7 ~]# vim vm.sh
cat <<EOF
+--------------------------+
| vmware manager |
+--------------------------+
| by zls |
+--------------------------+
| 1. Install KVM |
| 2. Install or Reset C6.5 |
| 3. Install or Reset C7.4 |
| 5. Instqll or Reset W7 |
| 6. Remove all |
| q. quit |
+--------------------------+
EOF
示例6:
[root@centos7 ~]# vim fruit.sh
# !/bin/sh
menu(){
cat <<EOF
+------------+
| 1 | apple |
+---+--------+
| 2 | pear |
+---+--------+
| 3 | banana |
+---+--------+
| 4 | cherry |
+---+--------+
EOF
read -p "please input a num: " fruit
}
usage(){
echo "USAGE:請輸入水果編號"
exit 1
}
color(){
case "$fruit" in
1)
echo -e "\E[1;31mapple \E[0m"
;;
2)
echo -e "\E[1;20mpear \E[0m"
;;
3)
echo -e "\E[1;33mbanana \E[0m"
;;
4)
echo -e "\E[1;35mcherry \E[0m"
;;
*)
usage
esac
}
menu
color
示例7:多條命令重定向
[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) &
[1] 6378
[root@centos7 ~]# (while :; do date; sleep 2; done) &>date.txt &
[root@centos7 ~]# jobs
[1]+ 運行中 ( while :; do date; sleep 2;
done ) &>/date.txt &
示例8:后台進程重定向
(while :; do date; sleep 2; done) &>/dev/null &
擴展知識: subshell
[root@centos7 ~]# cd /boot; ls
#subshell 中執行
[root@centos7 ~]# (cd /boot; ls)
#如果不希望某些命令的執行對當前 shell 環境產生影響,請在subshell中執行
四、管道技術 |
1、什么是管道?
管道操作符號 "|" 連接左右兩個命令, 將左側的命令的標准輸出, 交給右側命令的標准輸入。
注意:無法傳遞標准錯誤輸出至后者命令。
2、管道流程示意圖
案例1:將/etc/passwd
中的UID
取出並按大小排序
[root@centos7 ~]# awk -F : '{print $3}' /etc/passwd|sort -n
[root@centos7 ~]# sort -t":" -k3 -n /etc/passwd
[root@centos7 ~]# sort -t":" -k3 -n /etc/passwd -r
[root@centos7 ~]# sort -t":" -k3 -n /etc/passwd |head
案例2:統計當前/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
案例3:統計最占cpu的5個進程
[root@centos7 ~]# ps aux --sort=-%cpu |head -6
案例4:統計網站訪問量top20
#思路: 打印所有訪問的連接 | 過濾訪問網站的連接 | 打印用戶的 IP | 排序 | 去重
[root@centos7 ~]# yum -y install httpd
[root@centos7 ~]# systemctl start httpd
[root@centos7 ~]# systemctl stop firewalld
[root@centos7 ~]# ss -an |grep :80 |awk -F":" '{print $8}' |sort |uniq -c
[root@centos7 ~]# ss -an |grep :80 |awk -F":" '{print $8}' |sort |uniq -c |sort -k1 -rn |head -n 20
案例5:取出cpu已使用的百分比,只顯示數字
[root@centos7 ~]# df -h |awk -F '[ %]+' 'NR==2 {print $5}'
4
案例6:打印當前所有ip
[root@centos7 ~]# ip addr |grep 'inet ' |awk '{print $2}' |awk -F"/" '{print $1}'
127.0.0.1
192.168.69.112
案例7:打印根分區已用空間的百分比(僅打印數字)
[root@centos7 ~]# df |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
192.168.69.112
192.168.122.1
[root@zls ~]# cat ip.txt
inet 127.0.0.1/8 scope host lo
inet 192.168.69.112/24 brd 192.168.69.255 scope global ens32
inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0
重定向與tee的區別
[root@centos7 ~]# date > date.txt #直接將內容寫入date.txt文件中
[root@centos7 ~]# date |tee date.txt #命令執行會輸出至屏幕,但會同時保存一份至date.txt文件中
六、參數傳遞xargs
有些命令沒有標准輸入,通過|xargs 將前面命令的標准輸出,交給后面命令來處理。
xargs參數傳遞,主要讓一些不支持管道的命令可以使用管道技術。
將參數列表轉換成小塊分段傳遞給其他命令,讀入stdin的數據轉換為參數添加至命令后面,讓一些不支持管道的命令可以使用管道。
[root@centos7 ~]# grep "/sbin/nologin" /etc/passwd | wc -l
33
[root@centos7 ~]# head -5 /etc/passwd|tail -1
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
#使用grep過濾輸出信息
[root@centos7 ~]# ls -l /etc |grep pass
-rw-r--r-- 1 root root 4653 Dec 2 15:54 passwd
-rw-r--r--. 1 root root 4606 Dec 2 15:54 passwd-
-rw-r--r--. 1 root root 1454 Sep 23 2014 passwd.OLD
//管道和標准輸出以及標准錯誤輸出, 使用普通用戶執行如下命令
find /etc/ -name "p*"|grep passwd
find /etc/ -name "p*"|grep passwd > a
find /etc/ -name "p*"|grep passwd > b
find /etc/ -name "p*"|grep passwd &> ab
注意事項:
1)在管道后面的命令,都不應該再寫文件名。
2)在管道中只有標准輸出才可以傳遞下一個命令,標准錯誤輸出會直接輸出終端顯示,建議在使用管道前將標准錯誤輸出重定向。
例如: find /etc -name "*.conf" 2>/dev/null | grep rc
3)有些命令不支持管道技術,但是可以通過
例如: which cat|xargs ls-l
例如: ls |xargs rm -rvf
例如: ls |xargs cp -rvft /tmp/ -> ls | xargs -I {} cp -rvf {} /tmp
例如: ls |xargs mv -t /tmp/ -> ls | xargs -I {} mv {} /tmp