shell script
shell腳本的作用與執行
如果有一系列你經常使用的Linux命令,你可以把它們存儲在一個文件裏,shell可以讀取這個文件並順序執行其中的命令,這樣的文件被稱爲腳本文件。shell 腳本按行解釋。
- shell腳本的編寫
Shell 腳本是純文本文件,可以使用任何文本編輯器編寫
Shell 腳本通常是以 .sh 作爲後綴名 - Shell 腳本的執行
使用如下命令:
chmod +x script_name
./script_name
或
bash script_name
shell腳本格式
- 第一行:指定用哪個程序來編譯和執行腳本。
#!/bin/bash
#!/bin/sh
#!/bin/csh
- 一個 shell 腳本通常由一組 Linux 命令、shell 命令、控制結構和註釋語句構成。
- 註釋:以 “ # ” 開頭,可獨佔一行,或跟在語句的後面。
例1、
#!/bin/bash
# This is the first Bash shell program
# ScriptName: greetings.sh
echo
echo –e "Hello $LOGNAME, \c"
echo "it's nice talking to you."
echo "Your present working directory is:"
pwd # Show the name of present directory
echo
echo –e "The time is `date +%T`!. \nBye"
echo
如上一個簡單的shell腳本,使用
bash greeting.sh
或者
chmod +x greetings.sh
./greetings
運行這個腳本
echo命令
可以看到例1中有很多echo命令,下面本文從echo開始,介紹shell腳本編寫常用命令。
echo:
- 命令簡介
功能說明:顯示文字。
語 法:echo [-ne][字符串]
或 echo [–help][–version]
補充說明:echo會將輸入的字符串送往標準輸出。輸出的字符串間以空白字符隔開, 並在最後加上換行號。
-n 不進行換行
-e 若字符串中出現以下字符,則特別加以處理,而不會將它當成一般文字輸出 \n 換行 \b 空格… - 參數說明
-n 不要在最後自動換行
-e 若字符串中出現以下字符,則特別加以處理,而不會將它當成一般文字輸出:
\a 發出警告聲;
\b 刪除前一個字符;
\c 最後不加上換行符號;
\f 換行但光標仍舊停留在原來的位置;
\n 換行且光標移至行首;
\r 光標移至行首,但不換行;
\t 插入tab;
\v 與\f相同;
\ 插入\字符;
\nnn 插入nnn(八進制)所代表的ASCII字符;
–help 顯示幫助
–version 顯示版本信息
read命令
例2
#!/bin/bash
# This script is to test the usage of read
# Scriptname: ex4read.sh
echo "=== examples for testing read ==="
echo -e "What is your name? \c"
read name
echo "Hello $name"
echo
echo -n "Where do you work? "
read
echo "I guess $REPLY keeps you busy!"
echo
read -p "Enter your job title: "#自動讀給REPLY
echo "I thought you might be an $REPLY."
echo
echo "=== End of the script ==="
- 命令介紹
read variable #讀取變量給variable
read x y #可同時讀取多個變量
read #自動讀給REPLY
read –p “Please input: ”
#自動讀給REPLY
條件測試
條件測試可以根據某個特定條件是否滿足,來選擇執行相應的任務。
Bash 中允許測試兩種類型的條件:
命令成功或失敗,表達式爲真或假
任何一種測試中,都要有退出狀態(返回值),退出狀態爲 0 表示命令成功或表達式爲真,非0 則表示命令失敗或表達式爲假。
狀態變量 $? 中保存命令退出狀態的值,如例3所示:
例3
grep $USER /etc/passwd
echo $?
grep hello /etc/passwd; echo $?
test命令——測試表達式的值
表達式測試包括字符串測試、整數測試和文件測試。
通常用 test 命令來測試表達式的值,如例4所示:
例4
x=5; y=10
test $x -gt $y
echo $?
test 命令可以用 方括號 來代替,如例5:
例5:
x=5; y=10
[ $x -gt $y ]
echo $?
注意:方括號前後要留空格!
- 字符串測試
例如:
name=Tom; [ -z $name ]; echo $?
name2=Andy; [ $name = $name2 ] ; echo $?
整數測試
邏輯測試
文件測試
文件是否存在,文件屬性,訪問權限等。
常見的文件測試操作符
- 檢查空格
方法一:[ "$name" = "" ]
方法二:[ ! "$name" ]
方法三:[ "X${name}" != "X" ]
if 條件語句
例6:
if expr1 # 如果expr1 爲真(返回值爲0)
then # 那麼
commands1 # 執行語句塊 commands1
elif expr2 # 若expr1 不真,而expr2 爲真
then # 那麼
commands2 # 執行語句塊 commands2
... ... # 可以有多個 elif 語句
else # else 最多只能有一個
commands4 # 執行語句塊 commands4
fi # if 語句必須以單詞 fi 終止
幾點說明:
1. elif 可以有任意多個(0 個或多個)
2. else 最多只能有一個(0 個或 1 個)
3. if 語句必須以 fi 表示結束
4. expr 通常爲條件測試表達式;也可以是多個命令,以最後一個命令的退出狀態爲條件值。
5. commands 爲可執行語句塊,如果爲空,需使用 shell 提供的空命令 “ : ”,即冒號。該命令不做任何事情,只返回一個退出狀態 0
6. if 語句可以嵌套使用
例7:使用if語句比較標準輸入中兩個數的大小
#!/bin/bash
# scriptname: ex4if.sh
#
echo -n "Please input x,y: "
read x y
echo "x=$x, y=$y"
if (( x > y )); then
echo "x is larger than y"
elif (( x == y)); then
echo "x is equal to y"
else
echo "x is less than y"
fi
例8:if語句判斷文件及其權限
#!/bin/bash
# Using the new style test command: [[ ]]
# filename: perm_check2.sh
#
file=./testing
if [[ -d $file ]]
then
echo "$file is a directory"
elif [[ -f $file ]]
then
if [[ -r $file && -w $file && -x $file ]]
then # nested if command
echo "You have read,write,and execute permission on $file."
fi
else
echo "$file is neither a file nor a directory. "
fi
例9:判斷某個文件中是否存存在某個字符串
#!/bin/bash
# Using the new style test command: [[ ]]
# filename: perm_check2.sh
#
file=./testing
if [[ -d $file ]]
then
echo "$file is a directory"
elif [[ -f $file ]]
then
if [[ -r $file && -w $file && -x $file ]]
then # nested if command
echo "You have read,write,and execute permission on $file."
fi
else
echo "$file is neither a file nor a directory. "
fi
case 選擇語句
語法結構
case expr in # expr 爲表達式,關鍵詞 in 不要忘!
pattern1) # 若 expr 與 pattern1 匹配,注意括號
commands1 # 執行語句塊 commands1
;; # 跳出 case 結構
pattern2) # 若 expr 與 pattern2 匹配
commands2 # 執行語句塊 commands2
;; # 跳出 case 結構
... ... # 可以有任意多個模式匹配
*) # 若 expr 與上面的模式都不匹配
commands # 執行語句塊 commands
;; # 跳出 case 結構
esac # case 語句必須以 esac 終止
幾點說明
1. 表達式 expr 按順序匹配每個模式,一旦有一個模式匹配成功,則執行該模式後面的所有命令,然後退出 case。
2. 如果 expr 沒有找到匹配的模式,則執行缺省值 “ ) ” 後面的命令塊 ( 類似於 if 中的 else ); “ ) ” 可以不出現。
3.所給的匹配模式 pattern 中可以含有通配符和“ | ”
4. 每個命令塊的最後必須有一個雙分號,可以獨佔一行,或放在最後一個命令的後面。
例9:case語句舉例
#!/bin/bash
# test case
# scriptname: yes_no.sh
#
echo -n "Do you wish to proceed [y/n]: "
read ans
case $ans in
y|Y|yes|Yes)
echo "yes is selected"
;;
n|N|no|No)
echo "no is selected"
;;
*)
echo "`basename $0`: Unknown response"
exit 1
;;
esac
for 循環語句
語法結構:
for variable in list
# 每一次循環,依次把列表 list 中的一個值賦給循環變量
do # 循環開始的標誌
commands # 循環變量每取一次值,循環體就執行一遍
done # 循環結束的標誌
幾點說明
列表 list 可以是命令替換、變量名替換、字符串和文件名列表 ( 可包含通配符 )
for 循環執行的次數取決於列表 list 中單詞的個數
for 循環體中一般要出現循環變量,但也可以不出現
循環執行過程:
執行第一輪循環時,將 list 中的第一個詞賦給循環變量,並把該詞從 list 中刪除,然後進入循環體,執行 do 和 done 之間的命令。下一次進入循環體時,則將第二個詞賦給循環變量,並把該詞從 list 中刪除,再往後的循環也以此類推。當 list 中的詞全部被移走後,循環就結束了。