管道及重定向
|
管道符 前面的結果給後面執行
‘>>‘
<<標準輸入 描述符 0
-------> /proc/ ----> fd 文件描述符位置
lsof -p 4510 查看打開文件的描述符
vim mima.sh
#!/bin/bash
read -p “請你輸入用戶名稱:” username 讀用戶的標準輸入
stty -echo
read -p “請你輸入用戶密碼:” pass
stty echo
echo "$useradd 趕緊把你的銀行卡換掉,我已經知道你的密碼了,你的密碼是:“ $pass ”對不對“標準正確輸出 描述符 1
標準錯誤輸出 描述符 2 3+(進程在執行過程中打開的其他的文件)
find / -size +3G ’1‘ 是正確 ‘2’是錯誤
只想要正確不要錯誤# find / -size +3G 2> /dev/null 定向到垃圾桶
正確和錯誤都輸入到相同位置# find / -size +3G &>list.txt //混合輸出
都丟到一起# ls find / -size +3G 1> /dev/null 2>&1>
前面什麽都不寫比表示1
輸出重定向(追加)# date >> date.txt輸出重定向(覆蓋)# date 1> date.txt屏幕回顯
取消屏幕回顯:#stty -echo
文件描述符
file descriptors 簡稱fd 或 Process I/O channels
進程使用文件描述符來管理打開的文件
[root@wing ~]# ls /proc/$$/fd $$當前的進程號
0 1 2 3 4
- 手動創建 /dev/null
mknod -m 666 /dev/null c 1 3
主設備號相同: 表示為同一種設備類型,也可以認為kernel使用的是相同的驅動
從設備號:在同一類型設備中的一個序號
普通文件和設備文件:
c
rw-rw-rw- 1 root root 1
, 3
8月 1 06:36 /dev/nullb
rw-rw---- 1 root disk 8
, 1
8月 1 06:36 /dev/sda1-rw-r--r--. 1 root root 158 6月 7 2013 /etc/hos
案例7:腳本中使用重定向
[root@wing ~]# vim ping1.sh
ping -c1 10.18.40.100
if [ $? -eq 0 ];then
echo "10.18.40.100 is up."
else
echo "10.18.40.100 is down!"
fi
[root@wing ~]# vim ping1.sh
[root@wing ~]# chmod +x ping1.sh
[root@wing ~]# ./ping1.sh
[root@wing ~]# vim ping1.sh
ping -c1 10.18.40.100 &>/dev/null
if [ $? -eq 0 ];then
echo "10.18.40.100 is up."
else
echo "10.18.40.100 is down!"
fi
案例8:腳本中使用重定向
[root@wing ~]# vim ping2.sh
ping -c1 10.18.40.100&>/dev/null
if [ $? -eq 0 ];then
echo "10.18.40.100 is up.">>up.txt
else
echo "10.18.40.100 is down!" >>down.txt
fi
[root@wing ~]# vim ping2.sh
[root@wing ~]# chmod +x ping1.sh
[root@wing ~]# ./ping2.sh
輸入重定向
標準輸入: < 等價 0<
案例1:
[root@wing ~]# mail alice //沒有改變輸入的方向,默認鍵盤
Subject: hello
1111
2222
3333
.
EOT
[root@wing ~]# su - alice
[alice@wing ~]$ mail
Heirloom Mail version 12.5 7/5/10. Type ? for help.
"/var/spool/mail/alice": 1 message 1 new>
N 1 root Mon Jul 31 15:16 20/617 "hello"
[root@wing ~]# mail -s "test01" alice < /etc/hosts //輸入重定向,來自於文件案例2:
[root@wing ~]# grep ‘root‘ //沒有改變輸入的方向,默認鍵盤,此時等待輸入...
yang sss
sssrootssss..
sssrootssss..
[root@wing ~]# grep ‘root‘ < /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
案例3:
[root@wing ~]# dd if=/dev/zero of=/file1.txt bs=1M count=2
[root@wing ~]# dd </dev/zero >/file2.txt bs=1M count=20
案例4:mysql表結構導入
[root@wing ~]# mysql -uroot -p123 < bbs.sql
案例5:at
[root@wing ~]# at now +5 min
at> useradd yang99
at> <EOT>
job 1 at Mon Jul 31 15:29:00 2017
[root@wing ~]# vim at.txt
sudo useradd yang100
sudo useradd yang102
[root@wing ~]# at now +2 min <a.txt
job 2 at Mon Jul 31 15:27:00 2017綜合案例1: 利用重定向建立多行的文件
手動執行shell命令
[root@wing ~]# echo "111" > file1.txt
[root@wing ~]# cat file1.txt
111
[root@wing ~]# cat>
file2.txt
111
222
333
444^D
[root@wing ~]# cat file2.txt
請問:file2.txt有幾行?
[root@wing ~]# cat >>
file3.txt
aaa
bbb
ccc
ddd^D
[root@wing ~]# cat file3.txt
請問:file3.txt有幾行?
[root@wing ~]# cat >>file4 <<EOF
- > 111
- > 222
- > 333
-
> EOF
[root@wing ~]# cat file4
111
222
333綜合案例2: 利用重定向建立多行的文件
腳本script創建多行文件
[root@wing ~]# vim create_file.sh
cat >>file200.txt <<EOF
111
222
333
yyy
ccc
EOF
[root@wing ~]# bash create_file.sh
[root@wing ~]# cat file200.txt
111
222
333
yyy
ccc
綜合案例3: 腳本中利用重定向打印消息
[root@wing ~]# cat create_file.sh
cat <<-EOF
111
222
333
yyy
ccc
EOF
[root@wing ~]# bash create_file.sh
111
222
333
yyy
ccc
[root@wing ~]# vim virtual.sh
cat <<-EOF虛擬機基本管理 v4.0 by wing 1. 安裝KVM 2. 安裝或重置CentOS-6.8 3. 安裝或重置CentOS-7.3 4. 安裝或重置RHEL-6.4 5. 安裝或重置Windows-7 6. 刪除所有虛擬機 q. 退出管理程序
EOF綜合案例4
[root@wing ~]# ls; date &>/dev/null //希望兩條命令輸出都重定向 ??
[root@wing ~]# ls &>/dev/null; date &>/dev/null
[root@wing ~]# (ls; date) &>/dev/null
[root@wing ~]# (while :; do date; sleep 2; done)&
//在後臺運行,但輸出依然在前臺終端
[1] 6229
[root@wing ~]# 2017年 08月 01日 星期二 10:12:42 CST
2017年 08月 01日 星期二 10:12:44 CST
2017年 08月 01日 星期二 10:12:46 CST
2017年 08月 01日 星期二 10:12:48 CST
2017年 08月 01日 星期二 10:12:50 CST
[root@wing ~]# (while :; do date; sleep 2; done) &>
date.txt &
[root@wing ~]# tailf /date.txt
2017年 08月 01日 星期二 10:15:29 CST
2017年 08月 01日 星期二 10:15:31 CST
2017年 08月 01日 星期二 10:15:33 CST
2017年 08月 01日 星期二 10:15:35 CST
2017年 08月 01日 星期二 10:15:37 CST
2017年 08月 01日 星期二 10:15:39 CST
2017年 08月 01日 星期二 10:15:41 CST
[root@wing ~]# jobs
[1]+ 運行中 ( while :; do
date; sleep 2;
done ) &>/date.txt &
[root@wing ~]# kill %1
[root@wing ~]# jobs
後面課程學習安裝源碼軟件時:
[root@wing ~]# (./configure && make && make install) &>/dev/null 擴展點:subshell
==當前shell中執行==
[root@wing ~]# cd /boot; ls
config-3.10.0-514.el7.x86_64
efi
grub
grub2
initramfs-0-rescue-a024cb8d031d445580a7b5aaf92a9ca0.img
initramfs-3.10.0-514.el7.x86_64.img
initrd-plymouth.img
symvers-3.10.0-514.el7.x86_64.gz
System.map-3.10.0-514.el7.x86_64
vmlinuz-0-rescue-a024cb8d031d445580a7b5aaf92a9ca0
vmlinuz-3.10.0-514.el7.x86_64
[root@wing boot]#==在subshell中執行==
[root@wing boot]# cd
[root@wing ~]# (cd /boot; ls)
config-3.10.0-514.el7.x86_64
efi
grub
grub2
initramfs-0-rescue-a024cb8d031d445580a7b5aaf92a9ca0.img
initramfs-3.10.0-514.el7.x86_64.img
initrd-plymouth.img
symvers-3.10.0-514.el7.x86_64.gz
System.map-3.10.0-514.el7.x86_64
vmlinuz-0-rescue-a024cb8d031d445580a7b5aaf92a9ca0
vmlinuz-3.10.0-514.el7.x86_64
[root@wing ~]#如果不希望某些命令的執行對當前shell環境產生影響,請在subshell中執行!
[root@wing ~]# (umask 777; touch file8888)
[root@wing ~]# ll file8888
---------- 1 root root 0 Apr 12 22:11 file8888
[root@wing ~]# umask
0022
參數傳遞 Xargs
awk sed grep sort uniq less more xargs
xargs: ls cp rm
cat a.txt
/etc/passwd
cat a.txt | xargs ls
cat a.txt | xargs -i cp {} /tmp(把前面文件的東西一個一個給{}) cat a.txt | xargs -I {} cp {} /tmp (把前面文件裏的文件-I 給{} ,再給{})
cat files.txt |xargs ls -l (前面文件放到後面不需要加路徑就不用{})
find -exec cp {} /etc 全部 find | xargs cp
用一臺虛擬機的兩個終端
echo ‘ ‘ /dev/ps1
a.txt 裏面是目錄的話
cat a.txt | xargs cp -r /tmp例子:
#touch ac.txt
#vim ac.txt
/root/a.txt
#cat ac.txt | xargs -i cp {} /tmp
#cd /tmp
#ls
#cat a.txt
進程管道 piping
? Use redirection characters to control output to files
.
? Use piping to control output toother programs
.
files:
> 2> file1.txt /dev/pts/2 /dev/tty1 /dev/null /dev/sdaprograms:
|
進程管道用法:command1 | command2 |command3 |...
[root@wing ~]# ll /dev/ |less
[root@wing ~]# ps aux |grep ‘sshd‘
[root@wing ~]# rpm -qa |grep ‘httpd‘ //查詢所有安裝的軟件包,過濾包含httpd的包
[root@wing ~]# yum list |grep ‘httpd‘
案例1:將/etc/passwd中的用戶按UID大小排序
[root@wing ~]# sort -t":" -k3 -n /etc/passwd //以: 分隔,將第三列按字數升序
[root@wing ~]# sort -t":" -k3 -n /etc/passwd -r //逆序
[root@wing ~]# sort -t":" -k3 -n /etc/passwd |head
-t 指定字段分隔符--field-separator
-k 指定列
-n 按數值
案例2:統計出最占CPU的5個進程
[root@wing ~]# ps aux --sort=-%cpu |head -6
案例3:統計當前/etc/passwd中用戶使用的shell類型
思路:取出第七列(shell)|
排序(把相同歸類)|
去重
[root@wing ~]# awk -F: ‘{print $7}‘ /etc/passwd
[root@wing ~]# awk -F: ‘{print $7}‘ /etc/passwd |sort
[root@wing ~]# awk -F: ‘{print $7}‘ /etc/passwd |sort |uniq
[root@wing ~]# 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: 統計網站的訪問情況
top 20
思路: 打印所有訪問的連接 | 過濾訪問網站的連接 | 打印用戶的IP | 排序 | 去重
[root@wing ~]# yum -y install httpd
[root@wing ~]# systemctl start httpd
[root@wing ~]# systemctl stop firewalld
[root@wing ~]# 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@wing ~]# ss -an |grep :80 |awk -F":" ‘{print $8}‘ |sort |uniq -c |sort -k1 -rn |head -n 20
案例5: 打印當前所有IP
[root@wing ~]# ip addr |grep ‘inet ‘ |awk ‘{print $2}‘ |awk -F"/" ‘{print $1}‘
127.0.0.1
192.168.2.115
案例6:打印根分區已用空間的百分比(僅打印數字)
[root@wing ~]# df -P |grep ‘/$‘ |awk ‘{print $5}‘ |awk -F"%" ‘{print $1}‘
管道及重定向