Linux學習之Shell基礎——Bash基本功能——多命令順序執行與管道符
1、多命令順序執行()
多命令執行符 | 格式 | 作用 |
; | 命令1 ; 命令2 | 多個命令順序執行,命令之間沒有任何邏輯聯絡 |
&& | 命令1 && 命令2 | 邏輯與 當命令1正確執行,則命令2才會執行 當命令1執行不正確,命令2不會執行 |
|| | 命令1 || 命令2 | 邏輯或 當命令1執行不正確,則命令2才會執行 當命令1正確執行,則命令2不會執行 |
示例1:
[[email protected] ~]# ls ; date ; cd/user ; pwd abc cde install.log sh 模板 圖片 下載 桌面 anaconda-ks.cfg cdef install.log.syslog 公共的 視訊 文件 音樂 2018年 12月 16日 星期日 13:21:00 CST -bash: cd/user: 沒有那個檔案或目錄 /root [[email protected] ~]# 因為當前系統中,cd 目錄下沒有user,所以執行結果如第三行,但不影響後面的結果,所以最後一條命令結果正確輸出了
示例2:
擴充套件:dd命令是進行Linux中磁碟、分割槽、檔案等特殊內容複製的命令,比cp更強大,或特殊
[[email protected] ~]# dd if=輸入檔案 of=輸出檔案 bs=位元組數 count=個數
選項:
if=輸入檔案 :指定原始檔或源裝置
of=輸出檔案 :指定目標檔案或目標裝置
bs=位元組數 :指定一次輸入/輸出多少位元組,即把這些位元組看做一個數據塊
count=個數 :指定輸入/輸出多少個數據塊
例子:
[[email protected] ~]# date ; dd if=/dev/zero of=/root/testfile bs=1k count=100000 ; date
# 把/dev/zero 特殊檔案,就是0,;該命令相當於建立一個100MB的檔案testfile ,路徑為/root/testfile
每次向其中寫個0,每次為1k,並把建立磁碟的始末時間。用以測試當前系統建立一個100MB的檔案需要多久
[[email protected] ~]# date ; dd if=/dev/zero of=/root/testfile bs=1k count=100000 ; date
2018年 12月 16日 星期日 13:29:07 CST
記錄了100000+0 的讀入
記錄了100000+0 的寫出
102400000位元組(102 MB)已複製,2.15774 秒,47.5 MB/秒
2018年 12月 16日 星期日 13:29:09 CST
[[email protected] ~]#
示例3:
[[email protected] ~]# ls anaconda-ks.cfg && echo yes
[[email protected] ~]#ls /root/test || echo "no
[[email protected] ~]# 命令 && echo yes || echo no
注:以上內容用於判斷命令是否被正確執行,
主要用於shell程式設計中,用於系統自動判定某個命令是否正確被執行的後續邏輯
[[email protected] ~]# ls
abc cde install.log sh 公共的 視訊 文件 音樂
anaconda-ks.cfg cdef install.log.syslog testfile 模板 圖片 下載 桌面
[[email protected] ~]#
[[email protected] ~]# ls && echo yes
abc cde install.log sh 公共的 視訊 文件 音樂
anaconda-ks.cfg cdef install.log.syslog testfile 模板 圖片 下載 桌面
yes
[[email protected] ~]# ls gdasdga && echo yes
ls: 無法訪問gdasdga: 沒有那個檔案或目錄
[[email protected] ~]# ./con
-bash: ./con: 沒有那個檔案或目錄
[[email protected] ~]# ls
abc cde install.log sh 公共的 視訊 文件 音樂
anaconda-ks.cfg cdef install.log.syslog testfile 模板 圖片 下載 桌面
[[email protected] ~]# ./configure && echo yes || echo no
-bash: ./configure: 沒有那個檔案或目錄
no
[[email protected] ~]#
2、管道符
命令格式:
[[email protected] ~]# 命令1 | 命令2
#命令1的正確輸出作為命令2的操作物件
注意:命令1必須有正確的輸出,命令2才有可能執行
示例:
[[email protected] ~]# ll -a /etc/ | more
# ll -a /etc/ 的查詢結果 作為管道符後面的輸入,
#在此表示分屏顯示 /etc/ 下檔案的長格式資訊顯示
[[email protected] ~]# ll -a /etc/ | more
總用量 2064
drwxr-xr-x. 118 root root 12288 12月 16 10:44 .
dr-xr-xr-x. 28 root root 4096 12月 16 07:08 ..
drwxr-xr-x. 3 root root 4096 11月 25 22:55 abrt
drwxr-xr-x. 4 root root 4096 11月 25 23:06 acpi
-rw-r--r--. 1 root root 45 12月 10 08:57 adjtime
-rw-r--r--. 1 root root 1512 1月 12 2010 aliases
-rw-r--r--. 1 root root 12288 11月 25 23:24 aliases.db
drwxr-xr-x. 2 root root 4096 11月 25 23:03 alsa
drwxr-xr-x. 2 root root 4096 11月 25 23:07 alternatives
-rw-------. 1 root root 541 8月 24 2016 anacrontab
-rw-r--r--. 1 root root 245 11月 11 2010 anthy-conf
-rw-r--r--. 1 root root 148 1月 12 2016 asound.conf
-rw-r--r--. 1 root root 1 3月 22 2017 at.deny
drwxr-x---. 3 root root 4096 11月 25 23:07 audisp
drwxr-x---. 3 root root 4096 11月 25 23:07 audit
-rw-r--r--. 1 root root 13641 6月 20 00:20 autofs.conf
-rw-------. 1 root root 232 6月 20 00:20 autofs_ldap_auth.conf
-rw-r--r--. 1 root root 667 6月 20 00:20 auto.master
-rw-r--r--. 1 root root 524 6月 20 00:20 auto.misc
-rwxr-xr-x. 1 root root 1260 6月 20 00:20 auto.net
-rwxr-xr-x. 1 root root 687 6月 20 00:20 auto.smb
drwxr-xr-x. 2 root root 4096 11月 25 22:55 avahi
drwxr-xr-x. 2 root root 4096 11月 25 23:08 bash_completion.d
-rw-r--r--. 1 root root 2699 3月 22 2017 bashrc
drwxr-xr-x. 2 root root 4096 11月 25 23:27 blkid
drwxr-xr-x. 2 root root 4096 11月 25 23:02 bluetooth
drwxr-xr-x. 2 root root 4096 11月 25 22:53 bonobo-activation
-rw-r--r--. 1 root root 1780 10月 16 2009 cas.conf
-rw-r--r--. 1 root root 28 6月 26 22:52 centos-release
drwxr-xr-x. 2 root root 4096 11月 25 23:01 certmonger
drwxr-xr-x. 2 root root 4096 5月 11 2016 chkconfig.d
drwxr-xr-x. 5 root root 4096 11月 25 22:55 ConsoleKit
--More--
[[email protected] ~]# netstat -an | grep "ESTABLISHED"
3、grep 搜尋命令補充
[[email protected] ~]# grep 【選項】“搜尋內容” 檔名
選項:
-i : 忽略大小寫
-n :輸出行號
-v : 反向查詢
--color=auto 搜尋出的關鍵字用顏色顯示