[toc]linux
8.10 shell特殊符號cut命令shell
8.11 sort_wc_uniq命令bash
8.12 tee_tr_split命令ui
8.13 shell特殊符號下.net
相關測驗題目:http://ask.apelearn.com/question/5437code
擴展blog
[ ] * 任意個任意字符,通配符排序
[ ] ? 任意一個字符文檔
[ ] # 註釋字符get
[ ] \ 脫義字符
[ ] | 管道符
oot@localhost:~# a=1 root@localhost:~# b=2 root@localhost:~# c=$a$b root@localhost:~# echo $c 12 root@localhost:~# c=\$a\$b root@localhost:~# echo $c $a$b
root@localhost:~# cat /etc/passwd |head -2 root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin root@localhost:~# cat /etc/passwd |head -2 |cut -d ":" -f 1 //只取第一段 root bin root@localhost:~# cat /etc/passwd |head -2 |cut -d ":" -f 1,2 //取第一段,第二段 root:x bin:x root@localhost:~# cat /etc/passwd |head -2 |cut -c 4 t :
root@localhost:~# sort -r /etc/passwd xavi:x:1000:1000:xavi,xavi's office,62580558,62589906:/home/xavi:/bin/bash xavidsf:x:1001:1001:xavi:/home/xavidsf:/bin/bash usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin unbound:x:993:991:Unbound DNS resolver:/etc/unbound:/sbin/nologin tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin test:x:1002:1002::/home/test:/bin/bash
root@localhost:~# vi 2.txt root@localhost:~# cat 2.txt 2221dedd dede ded 1213 32e4 ded root@localhost:~# wc -l 2.txt 6 2.txt root@localhost:~# wc -m 2.txt 32 2.txt root@localhost:~# wc -w 2.txt 6 2.txt
root@localhost:~# sort 2.txt|uniq //必須先給文件排序,才能生效 1 11 1213 2 2221dedd 3 3232 32e4 444 ded dede
root@localhost:~# sort 2.txt|uniq -c //統計重複次數, 1 1 1 11 1 1213 2 2 1 2221dedd 2 3 1 3232 1 32e4 1 444 2 ded 1 dede
root@localhost:~# echo "dadsaddsdad" |tee a.txt dadsaddsdad root@localhost:~# sort 2.txt|uniq -c |tee a.txt 1 1 1 11 1 1213 2 2 1 2221dedd 2 3 1 3232 1 32e4 1 444 2 ded 1 dede
root@localhost:~# sort 2.txt|uniq -c |tee -a a.txt 1 1 1 11 1 1213 2 2 1 2221dedd 2 3 1 3232 1 32e4 1 444 2 ded 1 dede root@localhost:~# cat a.txt 1 1 1 11 1 1213 2 2 1 2221dedd 2 3 1 3232 1 32e4 1 444 2 ded 1 dede 1 1 1 11 1 1213 2 2 1 2221dedd 2 3 1 3232 1 32e4 1 444 2 ded 1 dede
[root@localhost ~]# echo "xavilinux" |tr '[al]' '[AL]' xAviLinux [root@localhost ~]# echo "xavilinux" |tr 'a' 'A' xAvilinux
-b 表示依據大小來分割文檔,單位爲byte
[root@localhost ~]# mkdir split_dir [root@localhost ~]# cd !$ cd split_dir [root@localhost split_dir]# cp /etc/passwd ./ [root@localhost split_dir]# split -b 500 passwd [root@localhost split_dir]# ls passwd xaa xab xac xad xae
查看起實際大小,du -sh查看的是塊,du -sb 按照byte查找大小
若是split不指定目標文件名,則會以xaa、xab……這樣的文件名來存取切割後的文件。指定目標文件名,示例以下:
[root@localhost split_dir]# rm -f x* //把前面分割好的文件所有刪除 [root@localhost split_dir]# split -b 100 passwd 123 [root@localhost split_dir]# ls 123aa 123ad 123ag 123aj 123am 123ap 123as 123av passwd 123ab 123ae 123ah 123ak 123an 123aq 123at 123aw 123ac 123af 123ai 123al 123ao 123ar 123au 123ax
-l 表示依據行數來分割文檔
[root@localhost split_dir]# rm -f 123* [root@localhost split_dir]# split -l 10 passwd //10行分割 [root@localhost split_dir]# wc -l * 46 passwd 10 xaa 10 xab 10 xac 10 xad 6 xae 92 總用量
$能夠用做變量前面的標識符,還能夠和!結合使用。
[root@localhost /]# touch 1.txt [root@localhost /]# ls 1.txt 1.txt [root@localhost /]# ls !$ ls 1.txt 1.txt
!$表示上條命令的最後一個變量,本例中上條命令最後是1.txt,那麼在當前命令下輸入!$則表示1.txt
在一行命令中運行兩個或兩個以上的命令,須要在命令之間加符號;。
[root@localhost /]# ls 1.txt ; wc -l 2.txt 1.txt 8 2.txt [root@localhost /]# ls 1.txt;wc -l 2.txt //命令之間不空格也是有效的, 1.txt 8 2.txt
符號~表明用戶的家目錄,root用戶的家目錄是/root,普通用戶的家目錄是/home/username。
把一條命令放到後臺執行,則須要加上符號&,它一般用於命令運行時間較長的狀況。好比,能夠用在sleep後,以下所示:
[root@localhost /]# sleep 30 & [2] 7047 [root@localhost /]# jobs [1]+ 已中止 wc -l(工做目錄:~/split_dir) [2]- 運行中 sleep 30 &
和>>分別表示取代和追加的意思。當咱們運行一個命令報錯時,報錯信息會輸出到當前屏幕。若是想重定向到一個文本,則要用重定向符號2>或者2>>,它們分別表示錯誤重定向和錯誤追加劇定向。
中括號內爲字符組合,表明字符組合中的任意一個,還能夠表示一個範圍(1-3,a-z)。
在上面剛剛提到了分號,用於多條命令間的分隔符。另外還有兩個能夠用於多條命令中間的特殊符號,那就是 「&&」 和 「||」 下面把這幾種狀況全列出:
command1 ; command2
command1 && command2
command1 || command2
使用 」;」 時,無論command1是否執行成功都會執行command2;
使用 「&&」 時,只有command1執行成功後,command2纔會執行,不然command2不執行;
使用 「||」 時,command1執行成功後command2 不執行,不然去執行command2,總之command1和command2總有一條命令會執行。
[root@localhost /]# ls 1a.txt; wc -l 2.txt ls: 沒法訪問1a.txt: 沒有那個文件或目錄 [2]- 完成 sleep 30 8 2.txt [root@localhost /]# ls 1.txt || wc -l 2.txt 1.txt [root@localhost /]# ls 1.txt && wc -l 2.txt 1.txt 8 2.txt [root@localhost /]# ls 1a.txt && wc -l 2.txt ls: 沒法訪問1a.txt: 沒有那個文件或目錄