一伐坏、重定向
標準輸入、標準正確輸出纯露、標準錯誤輸出
進程在運行的過程中根據(jù)需要會打開多個文件埠褪,每打開一個文件會有一個數(shù)字標識组橄。這個標識叫文件描述符罚随。
進程使用文件描述符來管理打開的文件(FD----file descriptors).
文件描述符:每打開一個程序都會有文件描述
0遵班,標準輸入(鍵盤)
1,標準輸出
2,標準錯誤汇在,
3+,進程在執(zhí)行過程中打開的其他文件雳锋。
&:表示正確錯誤混合輸出
二、輸出重定向 (覆蓋,追加)
> ----覆蓋
>> ----追加
正確輸出: 1> 1>> 等價于 > >>
錯誤輸出: 2> 2>>
2.1. 案例1:輸出重定向(覆蓋)
[root@biudefor ~]# date 1> date.txt #正確輸出--覆蓋
# 注意:如果 > 前面什么都不加默認為1盈罐,標準正確輸出榜跌。
2.2.案例2:輸出重定向(追加)
[root@biudefor ~]# date >> date.txt #正確輸出--追加
2.3. 案例3:錯誤輸出重定向
[root@biudefor ~]# ls /home/ /aaaaaaaaa >list.txt
ls: cannot access /aaaaaaaaa: No such file or directory
[root@biudefor ~]# ls /home/ /aaaaaaaaa >list.txt 2>error.txt #重定向到不同的位置
[root@biudefor ~]# cat error.txt
ls: cannot access /aaaaaaaaa: No such file or directory
2.4.正確和錯誤都輸入到相同位置
[root@biudefor ~]# ls /home/ /aaaaaaaaa &>list.txt #混合輸出到相同文件
2.5.重定向到空設(shè)備/dev/null
[root@biudefor ~]# ls /home/ /aaaaaaaaa >list.txt 2>/dev/null #空設(shè)備,將錯誤的輸出丟掉
[root@biudefor ~]# ls /home/ /aaaaaaaaa &>/dev/null #空設(shè)備盅粪,將正確與錯誤的輸出丟掉
2.6.腳本中使用重定向
實戰(zhàn)一(沒有使用重定向)
[root@biudefor ~]# vim ping1.sh
#!/bin/bash
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@biudefor ~]# chmod +x ping1.sh
[root@biudefor ~]# ./ping1.sh #執(zhí)行文件(執(zhí)行腳本)
PING 10.18.40.100 (10.18.40.100) 56(84) bytes of data.
--- 10.18.40.100 ping statistics ---
1 packets transmitted, 0 received, 100% packet loss, time 0ms
10.18.40.100 is down!
實戰(zhàn)二(使用重定向)
[root@biudefor ~]# vim ping1.sh
#!/bin/bash
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@biudefor ~]# ./ping1.sh
三钓葫、輸入重定向 <
標準輸入: < 等價 0<
語法:cat >> file5 <<EOF #可以寫到腳本或者文件里面
EOF:開始和結(jié)束的標記。
成對使用
結(jié)尾的另一個必須定格寫票顾。
echo會將輸入的內(nèi)容送往標準輸出(打哟「 )
echo 內(nèi)容 >> 文件名或腳本里面
實戰(zhàn)案例一:
[root@biudefor ~]# cat >file4 <<EOF
> 111
> 222
> 333
> 444
> EOF
[root@biudefor ~]# cat file4
111
222
333
444
實戰(zhàn)案例二:
利用重定向建立多行的文件 腳本創(chuàng)建多行文件
[root@biudefor ~]# vim create_file.sh
#!/bin/bash
cat >file200.txt <<EOF
111
222
333
yyy
ccc
EOF
[root@biudefor ~]# chmod +x create_file.sh
[root@biudefor ~]# ./create_file.sh
[root@biudefor ~]# cat file200.txt
111
222
333
yyy
ccc
四、管道 |
語法:command1 | command2 |command3 |...
實戰(zhàn)案例一
[root@biudefor ~]# rpm -qa |grep 'httpd' #查詢所有安裝的軟件包奠骄,過濾包含httpd的包
httpd-tools-2.4.6-90.el7.centos.x86_64
httpd-2.4.6-90.el7.centos.x86_64
實戰(zhàn)案例二
將/etc/passwd中的用戶按UID大小排序
[root@biudefor ~]# sort -t":" -k3 -n /etc/passwd #以: 分隔豆同,將第三列按字數(shù)升序
[root@biudefor ~]# sort -t":" -k3 -n /etc/passwd -r #以: 分隔,將第三列按字數(shù)降序
[root@biudefor ~]# sort -t":" -k3 -n /etc/passwd |head #以: 分隔含鳞,將第三列按字數(shù)升序看前十行
[root@biudefor ~]# sort -t":" -k3 -n /etc/passwd |tail #以: 分隔影锈,將第三列按字數(shù)升序看后十行
參數(shù)詳解:
sort 排序,默認升序
-t 指定分隔符
-k 指定列
-n 按數(shù)值
-r 降序
head 默認輸出前十行
head -15 默認輸出前十五行
tail 默認輸出后十行
tail -20 默認輸出后十五行
五蝉绷、參數(shù)傳遞:xargs
對:ls cp rm 管道不能執(zhí)行精居。所以通過xargs。
cat a.txt | xargs -i cp {} /目錄
{}:前面?zhèn)鬟^來的內(nèi)容
-i :為了讓大括號生效
目錄時 -r
解釋:前面?zhèn)鬟^來的東西交給大括號
cat file.txt |xargs ls -l
前面是目錄或者目錄的路徑潜必。 ls - l 后面可以不加大括號靴姿,直接執(zhí)行。
實戰(zhàn)案例一
[root@biudefor ~]# touch /home/file{1..5}
[root@biudefor ~]# vim files.txt
/home/file1
/home/file2
/home/file3
/home/file4
/home/file5
[root@biudefor ~]# cat files.txt |ls -l #不加xargs傳參磁滚,看輸出結(jié)果
[root@biudefor ~]# cat files.txt |rm -rvf #不加xargs傳參佛吓,看輸出結(jié)果
[root@biudefor ~]# cat files.txt |xargs ls -l
-rw-r--rwx. 1 root root 12 Nov 7 21:57 /home/file1
-rw-r--r--. 1 root root 0 Nov 7 21:57 /home/file2
-rw-r--r--. 1 root root 0 Nov 7 21:57 /home/file3
-rw-r--r--. 1 root root 0 Nov 7 21:57 /home/file4
-rw-r--r--. 1 root root 0 Nov 7 21:57 /home/file5
[root@biudefor ~]# cat files.txt |xargs rm -rvf
removed ‘/home/file1’
removed ‘/home/file2’
removed ‘/home/file3’
removed ‘/home/file4’
removed ‘/home/file5’
實戰(zhàn)案例二
[root@biudefor ~]# touch /home/file{1..5}
[root@biudefor ~]# cat files.txt |xargs -I {} ls -l {}
-rw-r--r--. 1 root root 0 Nov 7 22:07 /home/file1
-rw-r--r--. 1 root root 0 Nov 7 22:07 /home/file2
-rw-r--r--. 1 root root 0 Nov 7 22:07 /home/file3
-rw-r--r--. 1 root root 0 Nov 7 22:07 /home/file4
-rw-r--r--. 1 root root 0 Nov 7 22:07 /home/file5
[root@linux-server ~]# cat files.txt |xargs -I {} cp -rvf {} /tmp
‘/home/file1’ -> ‘/tmp/file1’
‘/home/file2’ -> ‘/tmp/file2’
‘/home/file3’ -> ‘/tmp/file3’
‘/home/file4’ -> ‘/tmp/file4’
‘/home/file5’ -> ‘/tmp/file5’