shell中的if else語句與文件查找find淺析
上篇文章中我們講述了shell腳本編程的初步入門,其中講到了shell編程中的順序執行,順序執行時一種簡單的小腳本,如果在編輯腳本的時候遇到要做出條件判斷執行的時候要怎么辦呢?我們學習過if之后你會發現這會很簡單。if 語句通過關系運算符判斷表達式的真假來決定執行哪個分支。
Shell 有三種 if … else 語句:
if … fi 語句;
if … else … fi 語句;
if … elif … else … fi 語句。
1) if … else 語句
if … else 語句的語法:
if [ expression ] then Statement(s) to be executed if expression is true fi
如果 expression 返回 true,then 后邊的語句將會被執行;如果返回 false,不會執行任何語句。
最后必須以 fi 來結尾閉合 if,fi 就是 if 倒過來拼寫,后面也會遇見。
注意:expression 和方括號([ ])之間必須有空格,否則會有語法錯誤。
示例:
[root@centos7 ~]# ./test.sh a is not equal to b [root@centos7 ~]# cat test.sh #!/bin/sh a=10 b=20 if [ $a == $b ] then echo "a is equal to b" fi if [ $a != $b ] then echo "a is not equal to b" fi
2) if … else … fi 語句
if … else … fi 語句的語法:
if [ expression ] then Statement(s) to be executed if expression is true else Statement(s) to be executed if expression is not true fi
如果 expression 返回 true,那么 then 后邊的語句將會被執行;否則,執行 else 后邊的語句。
示例:
[root@centos7 ~]# ./test1.sh a is not equal to b [root@centos7 ~]# cat test1.sh #!/bin/sh a=10 b=20 if [ $a == $b ] then echo "a is equal to b" else echo "a is not equal to b" fi
3) if … elif … fi 語句
if … elif … fi 語句可以對多個條件進行判斷,語法為:
if [ expression 1 ] then Statement(s) to be executed if expression 1 is true elif [ expression 2 ] then Statement(s) to be executed if expression 2 is true elif [ expression 3 ] then Statement(s) to be executed if expression 3 is true else Statement(s) to be executed if no expression is true fi
哪一個 expression 的值為 true,就執行哪個 expression 后面的語句;如果都為 false,那么不執行任何語句。
示例:
[root@centos7 ~]# cat test2.sh #!/bin/sh a=10 b=20 if [ $a == $b ] then echo "a is equal to b" elif [ $a -gt $b ] then echo "a is greater than b" elif [ $a -lt $b ] then echo "a is less than b" else echo "None of the condition met" fi [root@centos7 ~]# chmod +x test2.sh [root@centos7 ~]# ./test2.sh a is less than b
read:
在腳本編程中使用read命令來接受輸入,read從標準輸入中讀取值,給每個單詞分配一個變量,使用read來把輸入值分配給一個或多個shell變量
-p 指定要顯示的提示
-t TIMEOUT
read -p “Enter a filename: “ FILE
示例:
[root@centos7 bin]# cat creatuser.sh #!/bin/bash read -p "請出入一個用戶名:" username if id $username &> /dev/null; then echo "用戶$username 存在" getent passwd $username else echo "用戶不存在" echo "創建$username 用戶" useradd $username echo "用戶$username 創建成功" id $username fi [root@centos7 bin]# ./creatuser.sh 請出入一個用戶名:zanghl 用戶不存在 創建zanghl 用戶 用戶zanghl 創建成功 uid=1003(zanghl) gid=1003(zanghl) groups=1003(zanghl)
這里運行腳本后顯示出:“請出入一個用戶名:” 用戶手動輸入一個用戶名。
case esac語句:
case 語句匹配一個值或一個模式,如果匹配成功,執行相匹配的命令。case語句格式如下:
case 值 in 模式1) command1 command2 command3 ;; 模式2) command1 command2 command3 ;; *) command1 command2 command3 ;; esac
case工作方式如上所示。取值后面必須為關鍵字in,每一模式必須以右括號結束。取值可以為變量或常數。匹配發現取值符合某一模式后,其間所有命令開始執行直至 ;;。
[root@centos7 bin]# cat yesorno.sh #!/bin/bash read -p "請輸入 yes or no:" yn case $yn in [yY][eE][sS]|[yY]) echo "yes is write" ;; [nN][oO]|[nN]) echo "no is write" ;; *) echo "input error" ;; esac [root@centos7 bin]# ./yesorno.sh 請輸入 yes or no:yEs yes is write [root@centos7 bin]# ./yesorno.sh 請輸入 yes or no:n no is write
find:
每一種操作系統都有許多的文件組成,對于linux這樣“一切皆文件”的操作系統來說更是如此,大家應該都能很輕松使用windows下的文件查找功能,但是對linux這一功能可能并不是很熟悉,linux不像windows那樣有固定的文件名后綴,并且因為linux陣營下百家爭鳴的特性,一個相同的文件在不同的發行版,可能會有不同,所以如果你能牢牢掌握find命令的使用,你在摸索linux的道路上將會順利很多。同時你會發現linux下文件查找功能其實很簡單,而且要比windows下查找功能強大很多很多。
語法:
find [OPTION]… [查找路徑] [查找條件] [處理動作]
查找路徑:指定具體目標路徑;默認為當前目錄
查找條件:指定的查找標準,可以文件名、大小、類型、權限等標準進行;默認為找出指定路徑下的所有文件
處理動作:對符合條件的文件做操作,默認輸出至屏幕
根據文件名和inode查找:
-name "文件名稱":支持使用glob *, ?, [], [^]
-iname "文件名稱":不區分字母大小寫
-inum n 按inode號查找
-samefile name 相同inode號的文件
-links n 鏈接數為n的文件
-regex "PATTERN":以PATTERN匹配整個文件路徑字 符串,而不僅僅是文件名稱
根據屬主、屬組查找:
-user USERNAME:查找屬主為指定用戶(UID)的文件
-group GRPNAME: 查找屬組為指定組(GID)的文件
-uid UserID:查找屬主為指定的UID號的文件
-gid GroupID:查找屬組為指定的GID號的文件
-nouser:查找沒有屬主的文件
-nogroup:查找沒有屬組的文件
組合條件:
與:-a
或:-o
非:-not, !
德·摩根定律:
非(P 且 Q) = (非 P) 或 (非 Q)
非(P 或 Q) = (非 P) 且 (非 Q)
!A -a !B = !(A -o B)
!A -o !B = !(A -a B)
查找條件:
-perm [/|-]MODE
MODE: 精確權限匹配
/MODE:任何一類(u,g,o)對象的權限中只要能一位匹配即可,或關系,+ 從centos7開始淘
-MODE:每一類對象都必須同時擁有指定權限,與關系 0 表示不關注
find -perm 755 會匹配權限模式恰好是755的文件
下面我們看一下下面這些示例:
這些示例能帶給你對find的深刻理解
1、查找/var目錄下屬主為root,且屬組為mail的所有文件
find /var -user root -group mail
2、查找/var目錄下不屬于root、lp、gdm的所有文件
find /var -not \( -user root -o -user lp -o -user gdm \) -ls
find /var -not -user root -not -user lp -not -user gdm
這里的兩個命令是一樣的,根據德·摩根定律:非(P 或 Q) = (非 P) 且 (非 Q);
第二行的命令 -not -user root 與后面的 -not -user lp 中間連接的是 -a 只是默認沒有寫而已;
3、查找/var目錄下最近一周內其內容修改過,同時屬主不為 root,也不是postfix的文件
find /var -mtime -7 -a -not \( -user root -o -user postfix \)
4、查找當前系統上沒有屬主或屬組,且最近一個周內曾被訪 問過的文件
find / \( -nouser -o -nogroup \) -a -atime -7
5、查找/etc目錄下大于1M且類型為普通文件的所有文件
find /etc -size +1M -a -type f
6、查找/etc目錄下所有用戶都沒有寫權限的文件
find /etc ! -perm /222 -ls
7、查找/etc目錄下至少有一類用戶沒有執行權限的文件
find /etc ! -perm -111
8、查找/etc/init.d目錄下,所有用戶都有執行權限,且其它 用戶有寫權限的文件
find /etc/init.d/ -perm -111 -perm /002
原創文章,作者:zanghonglei,如若轉載,請注明出處:http://www.www58058.com/36341
結構框架清晰,層次分明,對當天學到的東西,總結的很詳細,詳略得當,可以把操作的執行結果放上來,這樣會更好哦。