歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> SHELL編程 >> shell_02基本操作

shell_02基本操作

日期:2017/3/1 16:15:23   编辑:SHELL編程
shell_02基本操作 算術操作(expr) --在bash中只能做整數的運算 + 加 - 減 * 乘 / 除(取整) % 取余 $(()) $[] expr 60 \* 60 \* 24 expr 60 \* 60 \* 7 expr 1 + 1 expr 1 * 1 expr $( 10 + 10 ) / 10 expr $((10 + 10)) / 10 expr 10 % 3 # expr $RANDOM % 10 # let "v=2**16" # echo $v 65536 退出狀態 在Linux系統中,每當命令執行完成後,系統都會返回一個退出狀態。該退出狀態用一整數值表示,用於判斷命令運行正確與否。 若退出狀態值為0,表示命令運行成功 若退出狀態值不為0時,則表示命令運行失敗 最後一次執行的命令的退出狀態值被保存在內置變量“$?”中,所以可以通過echo語句進行測試命令是否運行成功 0 表示運行成功,程序執行未遇到任何問題 1~125 表示運行失敗,腳本命令、系統命令錯誤或參數傳遞錯誤 126 找到了該命令但無法執行 >128 命令被系統強制結束 條件表達式: test命令 用途:測試特定的表達式是否成立,當條件成立時,命令執行後的返回值$?為0,否則為其他數值 格式: test 條件表達式 [ 條件表達式 ] test可以測試表示有哪些: 1、文件狀態 2、字符串的比對 3、整數的比對 4、多條件組合(|| && !)(-a -o !) ------------------------------------------------------ #man test -a = && -o = || ! 單個條件: [ ! 1 -eq 1 ] 兩個值的結果再取反 ,感歎號的優先級別最低,除非加括號; ! [ 1 -eq 1 ] 多個條件: [ 1 -eq 1 -a 10 -eq 10 ] = [ 1 -eq 1 ] && [ 10 -eq 10 ] 兩個條件成立,才正確; [ 1 -eq 1 -o 10 -eq 10 ] = [ 1 -eq 1 ] || [ 10 -eq 10 ] 取或; [ 1 -eq 1 -o 10 -eq 10 -o 1 -eq 1 ] = [ 1 -eq 1 ] || [ 10 -eq 10 ] || [ 1 -eq 1 ] ----------------------------------------------------- # test 1 = 1 # echo $? 0 # [ 1 = 1 ] --[] = test --單條件 # echo $? 0 # [ 1 = 1 ] && echo YES YES # [ 1 = 2 ] && echo YES --無返回 || -o or && -a and ! 條件的組合: [ 1 = 1 -a 1 = 2 ] 與組合 [ 1 = 1 ] && [ 1 = 2 ] 與組合 [ 1 = 1 ] || [ 1 = 2 ] 或組合 [ 1 = 1 -o 1 = 2 ] 或組合 邏輯運算符 1) 邏輯運算符主要包括邏輯非、邏輯與、邏輯或運算符,具體描述如下表所示: 邏輯操作符 描述 !expression 如果expression為假,則測試結果為真 expression1 a expression2 如果expression1和expression同時為真,則測試結果為真 expression1 o expression2 如果expression1和expression2中有一個為真,則測試條件為真 2) 下表是邏輯運算符的“真假表”,其中expr1和expr2為表達式,用於描述了一個測試條件。 expr1 expr2 !expr1 ! expr2 expr1 a expr2 expr1 o expr2 真 真 假 假 真 真 真 假 假 真 假 真 假 真 真 假 假 真 假 假 真 真 假 假 [ ! 1 -eq 1 ] 盡量用 -eq 不用 = ; -eq 當整數處理; = 當字符串處理;shell的內部處理; <字符串或串(String)是由數字、字母、下劃線組成的一串字符。一般記為 s=“a1a2···an”(n>=0)。它是編程語言中表示文本的數據類型。> 常見的表達式類型 1、測試文件狀態 2、字符串比較 3、整數值比較 4、邏輯測試 文件表達式: -a file 文件是否存在 # [ -a /etc ] && echo YES --目錄 YES # [ -a /etc/passwd ] && echo YES --文件 YES # [ -a /etc/passwdddd ] && echo YES --無返回值 ------------------------------------- -b file 文件存在,而且是設備文件 # [ -b /dev/sda ] && echo YES YES # [ -b /dev/passwd ] && echo YES --無返回值 ------------------------------------- -c file 文件存在,而且字符文件 # [ -c /dev/tty1 ] && echo YES YES # [ -c /etc/passwd ] && echo YES --無返回值 ------------------------------------- -d file 文件存在,而且類型是目錄 # [ -d /etc/ ] && echo YES YES # [ -d /etc/passwd ] && echo YES --無返回值 ------------------------------------- -e file 文件的真實文件是否存在。 ------------------------------------- -f file 文件存在,而且常規文件 # [ -f /etc/passwd ] && echo YES YES # [ -f /etc/ ] && echo YES --無返回值 ------------------------------------- -g file 文件存在,而且設置強制位 # find / -perm -2000 --查找系統中哪些文件擁有強制位 # [ -g /usr/bin/wall ] && echo YES YES # [ -g /usr/bin/vim ] && echo YES --無返回值 ------------------------------------- -h file 文件存在,而且是軟鏈接文件 ------------------------------------- -k file 擁有粘滯位目錄 [ -k /tmp ] && echo YES YES # [ -k /root ] && echo YES --無返回值 ------------------------------------- -p file 測試管道文件 # find / -type p --查找系統中有哪些管道文件 # [ -p /tmp/vmware-root.2 ] && echo YES YES # [ -p /etc/passwd ] && echo YES --無返回值 ------------------------------------- -r file 文件存在,而且當前用戶對此擁有讀的權限 # su - user01 --切換到普通用戶 $ [ -r /etc/passwd ] && echo YES YES $ [ -r /etc/shadow ] && echo YES --無返回值 ------------------------------------- -s file 文件存在,而且文件內容非空 # touch /opt/a.txt # [ -s /opt/a.txt ] && echo YES --無返回值 # [ -s /etc/passwd ] && echo YES YES ------------------------------------- -t 文件描述符(FD)是在一個終端打開的 ------------------------------------- -u file 擁有suid的文件 # find / -perm -4000 # [ -u /bin/mount ] && echo YES YES # [ -u /etc/passwd ] && echo YES --無返回值 ------------------------------------- -w file 測試文件寫權限 # [ -w /etc/passwd ] && echo YES YES # [ -w /etc/ ] && echo YES YES $ [ -w /etc/passwd ] && echo YES --無返回值 ------------------------------------- -x file 測試是否擁有執行權限 $ [ -x /bin/ls ] && echo YES YES $ [ -x /sbin/unix_update ] && echo YES --無返回值 ------------------------------------- 下面為大寫字母: ------------------------------------- -O file 測試文件擁有者是否是當前用戶 $ touch /tmp/a $ [ -O /tmp/a ] && echo YES YES $ [ -O /etc/passwd ] && echo YES --無返回值 ------------------------------------- -G file 對此文件屬組(主組) $ touch /tmp/a $ [ -G /tmp/a ] && echo YES YES $ [ -G /etc/passwd ] && echo YES --無返回值 ------------------------------------- -L file True if file exists and is a symbolic link. --文件存在而且是軟鏈接 ------------------------------------- -S file 測試對象是否是socket # [ -S /dev/log ] && echo YES YES # [ -S /etc/passwd ] && echo YES --無返回值 ------------------------------------- -N file 測試文件最後一次至今有無更改過 # [ -N /etc/passwd ] && echo YES --無返回值 # useradd a # [ -N /etc/passwd ] && echo YES YES ------------------------------------- file1 -nt file2 --file1是否比file2新 -n 比較哪個文件新(和t在一起) True if file1 is newer (according to odification date) than file2, or if file1 exists and file2 does not. # [ /etc/passwd -nt /var/log/messages ] && echo YES # [ /var/log/messages -nt /etc/passwd ] && echo YES YES ------------------------------------- file1 -ot file2 --測試filt1是否老於file2 True if file1 is older than file2, or if file2 exists and file1 does not. # [ /var/log/messages -ot /etc/passwd ] && echo YES YES ------------------------------------- file1 -ef file2 True if file1 and file2 refer to the same device and inode numbers. # [ /etc/init.d -ef /etc/rc.d/init.d ] && echo YES --可以用於測試兩個文件是否是硬鏈接的關系 YES ------------------------------------- 字符串表達式: -z string 字符串true,如果字符串是空的。 # [ -z '' ] && echo YES YES # [ -z 'aa' ] && echo YES --無返回值 ----------------- string -n string 字符串true,如果字符串不為空。 # [ -n 'aa' ] && echo YES YES # [ -n '' ] && echo YES --無返回值 ----------------- string1 == string2 True if the strings are equal. = may be used in place of == for strict POSIX compliance. # [ 'aa' == 'aa' ] && echo YES YES # [ 'aa' == 'ab' ] && echo YES ----------------- string1 != string2 True if the strings are not equal. # [ 'aa' != 'ab' ] && echo YES YES # [ 'aa' != 'aa' ] && echo YES --無返回值 ----------------- string1 < string2 True if string1 sorts before string2 lexicographically in the current locale. # [ aa \< aaa ] && echo YES YES # [ aaa \< ccc ] && echo YES YES string1 > string2 True if string1 sorts after string2 lexicographically in the current locale. 整數對比表達式: arg1 OP arg2 -eq equal等於 -ne not equal不等於 -lt lesser than小於 -le lesser equal小於等於 -gt granter then大於 -ge granter equal大於等於 # [ 1 -eq 1 ] && echo YES YES # [ 1 -eq 2 ] && echo YES # [ 1 -ne 2 ] && echo YES YES # [ 1 -lt 2 ] && echo YES YES # [ 2 -gt 1 ] && echo YES YES # [ 1 -le 1 ] && echo YES YES # [ 1 -le 2 ] && echo YES YES # [ 2 -ge 2 ] && echo YES YES # [ 3 -ge 2 ] && echo YES YES 表達式的邏輯組合: 1、&& -a 邏輯與 2、|| -o 邏輯或 3、! 邏輯非 [ 1 -gt 2 ] -a [ -f /etc/passwd ] [ 1 -gt 2 ] && [ -f /etc/passwd ] 或: [ 1 -gt 2 ] -o [ -f /etc/passwd ] [ 1 -gt 2 ] || [ -f /etc/passwd ] 非: [ 1 -gt 2 ] -a [ ! -f /etc/passwd ] 總結: 1、判斷文件類型 -f -O -u -L -h -nt -ot -k -s -S test -f /etc/passwd [ -f /etc/passwd ] 2、字符串 -z -n == != >= <= 3、整數 -eq -gt -ge -lt -le 結構化語句: if for while until case function -------------------------------------------- If 語句 --用於判斷一個點 if list; then list; [ elif list; then list; ] ... [ else list; ] fi The if list is executed. If its exit status is zero, the then list is executed. Otherwise, each elif list is executed in turn, and if its exit status is zero, the corresponding then list is executed and the command completes. Otherwise, the else list is executed, if present. The exit status is the exit sta- tus of the last command executed, or zero if no condition tested true. if 單分支 if [ ] ... then command01 ... fi 雙分支 if [ ] then command01 else command01 fi for --循環結構化語句(一般是給定區間或者范圍來取值) 格式1: for name [ in word ] ; do list ; done 格式2: for (( expr1 ; expr2 ; expr3 )) ; do list ; done for i in 1 2 3 do echo $i done for i in {1..10} do echo $i done for ((i=1;i<=10;i++)) do echo $i done while --當條件成立的時候循環 while : do echo hello done until --當條件不成立的時候循環 until [] do echo hello done continue/break while --當條件成立時循環 while [ 1 = 1 ] do echo "i love you forever" done # while : ; do echo 'i love you forever!!!'; sleep 3s ;done # while true ; do echo 'i love you forever!!!'; sleep 3s ;done while的另一種模式: # vim a.txt 123 abc 456 while read line do echo $line done<a.txt cat a.txt | while read line;do echo $line; done until --當條件不成立的時候循環 until [ 1 != 1 ] do command1 command2 done break --做循環時如果滿足條件強制可以跳出循環,此時循環結束 continue --滿足條件時也可以跳出循環,但可以循環下一個元素。 case --分支判斷語句(可以判斷一個面(區間)) case $1 in case1) command01 ;; case2) command02 ;; case3) command03 ..... *) esac 函數 1、在編寫Shell腳本程序時,將一些需要重復使用的命令操作,定義為公共使用的語句塊,即可稱為函數 2、合理使用Shell函數,可以使腳本內容更加簡潔,增強程序的易讀性,提高執行效率 function_name() { command1 command2 } function_name --調用函數 function function_name() { } 顯示選擇菜單: select name in [ a b c ] do done -------------------------------------------- 漢字屬於字符串 用字符串的格式 #sh -n xxx.sh 檢查語法 #sh -x xxx.sh 調試模式 ------------------------------------------- • bash shell 調試方法 • 打開調試模式 • $set -x • $bash -x shell.sh • 關閉調試模式 • $set +x sh -n test.sh --檢查語法(結構化語句的語法) sh -x test.sh --調試 如果參數過太多,出現這個報錯:too many arguments,需要使用xargs xargs (1) - build and execute command lines from standard input # awk -F: '{print $1}' /etc/passwd | xargs -i mkdir -p {}
Copyright © Linux教程網 All Rights Reserved