天天看點

Shell程式設計

Linux Shell Programming

參考

流程控制

if

if condition
then
    command1 
    command2
    ...
    commandN 
fi
           
if [ $(ps -ef | grep -c "ssh") -gt 1 ]; then echo "true"; fi

           
if condition
then
    command1 
    command2
    ...
    commandN
else
    command
fi
           
if condition1
then
    command1
elif condition2 
then 
    command2
else
    commandN
fi
           
num1=$[2*3]
num2=$[1+5]
if test $[num1] -eq $[num2]
then
    echo '兩個數字相等!'
else
    echo '兩個數字不相等!'
fi
           

for

for var in item1 item2 ... itemN
do
    command1
    command2
    ...
    commandN
done
           

while

while condition
do
    command
done
           

無限循環

while :
do
    command
done

while true
do
    command
done

for (( ; ; ))
           

until

until condition
do
    command
done
           

case

case 值 in
模式1)
    command1
    command2
    ...
    commandN
    ;;
模式2)
    command1
    command2
    ...
    commandN
    ;;
esac
           

跳出循環

break語句

continue語句

Shell 基本運算符

關系運算符

運算符 說明 舉例
-eq 檢測兩個數是否相等,相等傳回 true。 [ $a -eq $b ] 傳回 false。
-ne 檢測兩個數是否相等,不相等傳回 true。 [ $a -ne $b ] 傳回 true。
-gt 檢測左邊的數是否大于右邊的,如果是,則傳回 true。 [ $a -gt $b ] 傳回 false。
-lt 檢測左邊的數是否小于右邊的,如果是,則傳回 true。 [ $a -lt $b ] 傳回 true。
-ge 檢測左邊的數是否大于等于右邊的,如果是,則傳回 true。 [ $a -ge $b ] 傳回 false。
-le 檢測左邊的數是否小于等于右邊的,如果是,則傳回 true。 [ $a -le $b ] 傳回 true。
EQ 就是 EQUAL等于

NE 就是 NOT EQUAL不等于 

GT 就是 GREATER THAN大于  

LT 就是 LESS THAN小于 

GE 就是 GREATER THAN OR EQUAL 大于等于 

LE 就是 LESS THAN OR EQUAL 小于等于

           

布爾運算符

! 非運算,表達式為 true 則傳回 false,否則傳回 true。 [ ! false ] 傳回 true。
-o 或運算,有一個表達式為 true 則傳回 true。 [ $a -lt 20 -o $b -gt 100 ] 傳回 true。
-a 與運算,兩個表達式都為 true 才傳回 true。 [ $a -lt 20 -a $b -gt 100 ] 傳回 false。

邏輯運算符

&& 邏輯的 AND [[ $a -lt 100 && $b -gt 100 ]] 傳回 false

字元串運算符

= 檢測兩個字元串是否相等,相等傳回 true。 [ $a = $b ] 傳回 false。
!= 檢測兩個字元串是否相等,不相等傳回 true。 [ $a != $b ] 傳回 true。
-z 檢測字元串長度是否為0,為0傳回 true。 [ -z $a ] 傳回 false。
-n 檢測字元串長度是否為0,不為0傳回 true。 [ -n $a ] 傳回 true。
str 檢測字元串是否為空,不為空傳回 true。 [ $a ] 傳回 true。

檔案測試運算符

操作符
-b file 檢測檔案是否是塊裝置檔案,如果是,則傳回 true。 [ -b $file ] 傳回 false。
-c file 檢測檔案是否是字元裝置檔案,如果是,則傳回 true。 [ -c $file ] 傳回 false。
-d file 檢測檔案是否是目錄,如果是,則傳回 true。 [ -d $file ] 傳回 false。
-f file 檢測檔案是否是普通檔案(既不是目錄,也不是裝置檔案),如果是,則傳回 true。 [ -f $file ] 傳回 true。
-g file 檢測檔案是否設定了 SGID 位,如果是,則傳回 true。 [ -g $file ] 傳回 false。
-k file 檢測檔案是否設定了粘着位(Sticky Bit),如果是,則傳回 true。 [ -k $file ] 傳回 false。
-p file 檢測檔案是否是有名管道,如果是,則傳回 true。 [ -p $file ] 傳回 false。
-u file 檢測檔案是否設定了 SUID 位,如果是,則傳回 true。 [ -u $file ] 傳回 false。
-r file 檢測檔案是否可讀,如果是,則傳回 true。 [ -r $file ] 傳回 true。
-w file 檢測檔案是否可寫,如果是,則傳回 true。 [ -w $file ] 傳回 true。
-x file 檢測檔案是否可執行,如果是,則傳回 true。 [ -x $file ] 傳回 true。
-s file 檢測檔案是否為空(檔案大小是否大于0),不為空傳回 true。 [ -s $file ] 傳回 true。
-e file 檢測檔案(包括目錄)是否存在,如果是,則傳回 true。 [ -e $file ] 傳回 true。

Shell變量

推薦給所有變量加上花括号,這是個好的程式設計習慣。

删除變量

unset variable_name

提取子字元串

string="runoob is a great site"
echo ${string:1:4} # 輸出 unoo
           

傳遞參數

參數處理
$# 傳遞到腳本的參數個數
$* 以一個單字元串顯示所有向腳本傳遞的參數。
$$ 腳本運作的目前程序ID号
$! 背景運作的最後一個程序的ID号
$@ 與$*相同,但是使用時加引号,并在引号中傳回每個參數。
$- 顯示Shell使用的目前選項,與set指令功能相同。
$? 顯示最後指令的退出狀态。0表示沒有錯誤,其他任何值表明有錯誤。

$* 與 $@ 差別:

  • 相同點:都是引用所有參數。
  • 不同點:隻有在雙引号中展現出來。假設在腳本運作時寫了三個參數 1、2、3,,則 " * " 等價于 "1 2 3"(傳遞了一個參數),而 "@" 等價于 "1" "2" "3"(傳遞了三個參數)。

printf

printf  format-string  [arguments...]

           
#!/bin/bash
printf "%-10s %-8s %-4s\n" 姓名 性别 體重kg  
printf "%-10s %-8s %-4.2f\n" 郭靖 男 66.1234 
printf "%-10s %-8s %-4.2f\n" 楊過 男 48.6543 
printf "%-10s %-8s %-4.2f\n" 郭芙 女 47.9876  
           
序列
\a 警告字元,通常為ASCII的BEL字元
\b 後退
\c 抑制(不顯示)輸出結果中任何結尾的換行字元(隻在%b格式訓示符控制下的參數字元串中有效),而且,任何留在參數裡的字元、任何接下來的參數以及任何留在格式字元串中的字元,都被忽略
\f 換頁(formfeed)
\n 換行
\r 回車(Carriage return)
\t 水準制表符
\v 垂直制表符
\ 一個字面上的反斜杠字元
\ddd 表示1到3位數八進制值的字元。僅在格式字元串中有效
\0ddd 表示1到3位的八進制值字元

test

字元串測試

參數
等于則為真
不相等則為真
-z 字元串 字元串的長度為零則為真
-n 字元串 字元串的長度不為零則為真

Shell 函數

[ function ] funname [()]

{

    action;

    [return int;]

}
           

函數參數

在Shell中,調用函數時可以向其傳遞參數。在函數體内部,通過 $n 的形式來擷取參數的值,例如,$1表示第一個參數,$2表示第二個參數...

#!/bin/bash
# author:菜鳥教程
# url:www.runoob.com

funWithParam(){
    echo "第一個參數為 $1 !"
    echo "第二個參數為 $2 !"
    echo "第十個參數為 $10 !"
    echo "第十個參數為 ${10} !"
    echo "第十一個參數為 ${11} !"
    echo "參數總數有 $# 個!"
    echo "作為一個字元串輸出所有參數 $* !"
}
funWithParam 1 2 3 4 5 6 7 8 9 34 73
           

注意,\(10 不能擷取第十個參數,擷取第十個參數需要\){10}。當n>=10時,需要使用${n}來擷取參數。

代碼改變世界