高效率的使用Linux通常都需要結(jié)合命令的順序控制以及管道一起實(shí)現(xiàn)。這里主要總結(jié)一下Linux命令的執(zhí)行順序控制和管道的使用方法。
命令執(zhí)行順序控制
1.順序執(zhí)行多個(gè)命令
通過;把多個(gè)命令放在一起,可以達(dá)到順序執(zhí)行這幾個(gè)命令的效果。

2.有選擇執(zhí)行命令
順序執(zhí)行命令存在的弊端顯而易見,如果我們需要在前面命令執(zhí)行成功(或者滿足一定條件時(shí))才執(zhí)行后續(xù)的命令,我們就需要學(xué)會控制命令執(zhí)行與否。
&&符號表示左側(cè)命令執(zhí)行返回0時(shí),執(zhí)行右側(cè)命令;||表示左側(cè)命令執(zhí)行返回1時(shí),執(zhí)行右側(cè)命令(注意:與java等代碼中對應(yīng)符號邏輯相反)。
shiyanlou:~/ $ which cowsay [17:35:25]
cowsay not found
shiyanlou:~/ $ echo $?[17:51:13]
1
shiyanlou:~/ $ which cowsay && echo 2[17:51:26]
cowsay not found
shiyanlou:~/ $ which cowsay>/dev/null&& echo 2 [17:51:57]
shiyanlou:~/ $ sudo apt-get update;sudo apt-get install cowsay
shiyanlou:~/ $ which cowsay [17:57:06]
/usr/games/cowsay
shiyanlou:~/ $ echo $?[17:57:21]
0
shiyanlou:~/ $ which cowsay && echo 2[17:57:29]
/usr/games/cowsay
2
shiyanlou:~/ $ which cowsay>/dev/null && echo 2[17:57:35]
2
由上述代碼知,
- 命令執(zhí)行返回和命令執(zhí)行的打印內(nèi)容不是一個(gè)概念;
-
echo $?表示打印前一個(gè)命令執(zhí)行的返回值; -
>\dev\null表示把命令執(zhí)行中打印的內(nèi)容拋進(jìn)‘地獄’(/dev/null就是個(gè)地獄一般的無底洞);
下圖可以形象的描述這個(gè)過程:

管道的使用
管道是一種通信機(jī)制,通常用于進(jìn)程間的通信。它表現(xiàn)出來的形式就是把前面進(jìn)程的輸出(stdout)直接作為下個(gè)進(jìn)程的輸入(stdin)。
管道通常分為匿名管道和具名管道。我們在使用一些過濾程序時(shí),經(jīng)常會用到匿名管道,在命令行中由|分隔符表示。具名管道簡單說就是有名字的管道,通常只會在源程序中用到具名管道。這里我們主要介紹匿名管道的常見用法。
-
less命令
shiyanlou:~/ $ ll /etc | less [19:04:33]
total 816K
drwxr-xr-x 2 root root 4.0K Aug 17 12:41 ImageMagick
drwxr-xr-x 2 root root 4.0K Aug 17 12:44 R
drwxr-xr-x 10 root root 4.0K Aug 17 12:44 X11
-rw-r--r-- 1 root root 3.0K May 29 2015 adduser.conf
drwxr-xr-x 2 root root 12K Aug 18 18:04 alternatives
drwxr-xr-x 8 root root 4.0K Aug 17 12:43 apache2
drwxr-xr-x 3 root root 4.0K May 29 2015 apparmor
drwxr-xr-x 6 root root 4.0K Aug 17 12:44 apparmor.d
drwxr-xr-x 4 root root 4.0K Aug 17 12:39 apport
drwxr-xr-x 6 root root 4.0K Aug 18 17:52 apt
drwxr-xr-x 3 root root 4.0K Aug 17 12:44 authbind
-rw-r--r-- 1 root root 2.2K Apr 9 2014 bash.bashrc
-rw-r--r-- 1 root root 45 Mar 23 2014 bash_completion
drwxr-xr-x 2 root root 4.0K Aug 17 12:44 bash_completion.d
-rw-r--r-- 1 root root 356 Jan 2 2012 bindresvport.blacklist
-rw-r--r-- 1 root root 321 Apr 17 2014 blkid.conf
lrwxrwxrwx 1 root root 15 Sep 3 2015 blkid.tab -> /dev/.blkid.tab
drwxr-xr-x 3 root root 4.0K Aug 17 12:37 ca-certificates
-rw-r--r-- 1 root root 7.7K Aug 17 12:42 ca-certificates.conf
drwxr-xr-x 2 root root 4.0K Aug 17 12:41 calendar
drwxr-xr-x 2 root root 4.0K May 29 2015 console-setup
drwxr-xr-x 2 root root 4.0K Aug 17 12:44 cron.d
drwxr-xr-x 2 root root 4.0K Aug 17 12:44 cron.daily
drwxr-xr-x 2 root root 4.0K May 29 2015 cron.hourly
drwxr-xr-x 2 root root 4.0K May 29 2015 cron.monthly
drwxr-xr-x 2 root root 4.0K Aug 17 12:41 cron.weekly
-rw-r--r-- 1 root root 722 Feb 9 2013 crontab
:
-
cut命令
/etc/passwd文件中字段之間由:隔離,每個(gè)字段代表不同的意思(具體參見Linux中/etc/passwd文件詳解),如果我們只要查看用戶名和用戶的home目錄,則可以用cut命令實(shí)現(xiàn):
shiyanlou:~/ $ grep "shiyanlou" /etc/passwd | cut -d ':' -f 1,6 [19:25:24]
shiyanlou:/home/shiyanlou
cut更多用法如下:
shiyanlou:~/ $ grep "shiyanlou" /etc/passwd | cut -c -5 #取前5個(gè)字符(包含第5個(gè))
shiyanlou:~/ $ grep "shiyanlou" /etc/passwd | cut -c 2-5 #取2~5個(gè)字符(包含第5個(gè))
shiyanlou:~/ $ grep "shiyanlou" /etc/passwd | cut -c 5- #取后5個(gè)字符(包含第5個(gè))
shiyanlou:~/ $ grep "shiyanlou" /etc/passwd | cut -c 5 #取前第五個(gè)字符
-
grep命令
grep是個(gè)很強(qiáng)大的命令, 主要完成從文本或stdin中查找匹配字符串,它還可以結(jié)合正則表達(dá)式實(shí)現(xiàn)很復(fù)雜卻很高效的匹配和查找。
grep格式一般為:
grep -rnI "eldonzhao" ~
其中-r表示遞歸查找,-n表示顯示行號,-I表示忽略大小寫
shiyanlou:~/ $ export | grep -nI "shiyan$" [19:28:34]
shiyanlou:~/ $ export | grep -nI "yanlou$" [19:52:25]
1:HOME=/home/shiyanlou
5:LOGNAME=shiyanlou
8:MAIL=/var/mail/shiyanlou
17:USER=shiyanlou
-
wc命令
wc是個(gè)非常簡單小巧的計(jì)數(shù)工具。
shiyanlou:~/ $ wc /etc/passwd [19:56:34]
28 35 1370 /etc/passwd
shiyanlou:~/ $ wc -l /etc/passwd [19:59:02]
28 /etc/passwd #行數(shù)
shiyanlou:~/ $ wc -w /etc/passwd [19:59:07]
35 /etc/passwd #單詞書
shiyanlou:~/ $ wc -c /etc/passwd [19:59:12]
1370 /etc/passwd #字節(jié)數(shù)
shiyanlou:~/ $ wc -m /etc/passwd [19:59:21]
1370 /etc/passwd #字符數(shù)
shiyanlou:~/ $ wc -L /etc/passwd [19:59:27]
81 /etc/passwd #最長行字節(jié)數(shù)
shiyanlou:~/ $ ls -d /etc/*/ | wc -l[20:02:20]
103 #etc下所有目錄數(shù)
-
sort命令
該命令的功能就是將輸入按照一定方式排序,然后再輸出。它支持的排序方式有:字典排序、數(shù)字排序、按月份排序、隨機(jī)排序、反轉(zhuǎn)排序、指定字符串排序等。
- 默認(rèn)排序(字典排序)
shiyanlou:~/ $ cat /etc/passwd | sort [16:33:30]
backup:x:34:34:backup:/var/backups:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
games:x:5:60:games:/usr/games:/usr/sbin/nologin
- 反轉(zhuǎn)排序
shiyanlou:~/ $ cat /etc/passwd | sort -r [16:33:34]
www-data:x:33:33:www-data:/var/www:/usr/sbin/nologin
uucp:x:10:10:uucp:/var/spool/uucp:/usr/sbin/nologin
tomcat7:x:108:112::/usr/share/tomcat7:/bin/false
syslog:x:101:104::/home/syslog:/bin/false
- 按特定字段排序
shiyanlou:~/ $ cat /etc/passwd | sort -t ":" -k 3 -n [16:41:56]
root:x:0:0:root:/root:/bin/bash
libuuid:x:100:101::/var/lib/libuuid:
上述命令表示要排序/etc/passwd中內(nèi)容,排序字段為每行按:分割后的第三個(gè)字段。-t ':'表示按:分割,-k 3表示按照第三個(gè)字段排序,-n表示按照數(shù)字順序排序。
-
uniq命令
uniq命令是用于去除連續(xù)重復(fù)行的。
shiyanlou:~/ $ history | cut -c 8- | cut -d ' ' -f 1 | sort | uniq #去掉重復(fù)的行
cat
history
history|
sort
uniq
shiyanlou:~/ $ history | cut -c 8- | cut -d ' ' -f 1 | sort | uniq -D #打印所有重復(fù)的行
cat
cat
cat
cat
cat
cat
history
history
shiyanlou:~/ $ history | cut -c 8- | cut -d ' ' -f 1 | sort | uniq -d #打印重復(fù)的行
cat
history
history|
sort
shiyanlou:~/ $ history | cut -c 8- | cut -d ' ' -f 1 | sort | uniq -dc #打印重復(fù)的行和重復(fù)次數(shù)
6 cat
7 history
4 history|
2 sort