注意shell腳本中是需要空格隔離的,
如:if後一定要有空格,if [ $# -ne 2 ]正確;if[$#=2]錯誤;因爲空格是一個詞的分界;
1 shell腳本第一行都是
#!/bin/sh符號#!用來告訴系統它後面的參數是用來執行該文件的程序。在這個例子中我們使用/bin/sh來執行程序。
要使腳本可執行:
chmod +x filename
然後,您可以通過輸入: ./filename 來執行您的腳本。
2 變量
在shell編程中,所有的變量都由字符串組成,並且您不需要對變量進行聲明。要賦值給一個變量,您可以這樣寫:
變量名=值
取出變量值可以加一個美元符號($)在變量前面:#!/bin/sh #對變量賦值: a="hello world" # 現在打印變量a的內容: echo "A is:" echo $a
再如:echo "this is the ${num}nd"
3 shell腳本中的三類命令
1)UNIX命令
file somefile: 得到文件類型
2)管道, 重定向:
重定向:將命令的結果輸出到文件,而不是標準輸出(屏幕)。
> 寫入文件並覆蓋舊文件
>> 加到文件的尾部,保留舊文件內容。
3) 流程控制
3.1) if
Shell 有且只有三種 if ... else 語句:
- if ... fi 語句;
- if ... else ... fi 語句;
- if ... elif ... else ... fi 語句。
if ....; then .... elif ....; then .... else .... fi
通常用" [ ] "來表示條件測試。注意這裏的空格很重要。要確保方括號的空格。
[ -f "somefile" ] :判斷是否是一個文件
[ -x "/bin/ls" ] :判斷/bin/ls是否存在並有可執行權限
[ -n "$var" ] :判斷$var變量是否有值
[ "$a" = "$b" ] :判斷$a和$b是否相等
條件表達式
if [ -f file ] 如果文件存在
if [ -d ... ] 如果目錄存在
if [ -s file ] 如果文件存在且非空
if [ -r file ] 如果文件存在且可讀
if [ -w file ] 如果文件存在且可寫
if [ -x file ] 如果文件存在且可執行算術比較運算符
if [ int1 -eq int2 ] 如果int1等於int2
if [ int1 -ne int2 ] 如果不等於
if [ int1 -ge int2 ] 如果>=
if [ int1 -gt int2 ] 如果>
if [ int1 -le int2 ] 如果<=
if [ int1 -lt int2 ] 如果<if [ = ]
if [ != ]
如:
#!/bin/sh if [ "$SHELL" = "/bin/bash" ]; then echo "your login shell is the bash (bourne again shell)" else echo "your login shell is not bash but $SHELL" fi
3.2) case
case表達式可以用來匹配一個給定的字符串,而不是數字。
case ... in
...) do something here ;;
esac
如:
#!/bin/sh ftype=`file "$1"` case "$ftype" in "$1: Zip archive"*) unzip "$1" ;; "$1: gzip compressed"*) gunzip "$1" ;; "$1: bzip2 compressed"*) bunzip2 "$1" ;; *) error "File $1 can not be uncompressed with smartzip";; esac
case $pinmux_vo_select in bt1120) echo "use bt1120"; vo_bt1120_mode; ;; rmii) echo "use rmii"; net_rmii_mode; ;; *) echo "use mii"; net_mii_mode; ;; esac
3.3)while、for
while ...; do .... done 如:
#!/bin/bash while [ $# != 0 ];do echo "第一個參數爲:$1,參數個數爲:$#" shift done
輸入如下命令運行:run.sh a b c d e f
結果顯示如下:
第一個參數爲:a,參數個數爲:6
第一個參數爲:b,參數個數爲:5
第一個參數爲:c,參數個數爲:4
第一個參數爲:d,參數個數爲:3
第一個參數爲:e,參數個數爲:2
第一個參數爲:f,參數個數爲:1
從上可知 shift(shift 1) 命令每執行一次,變量的個數($#)減一(之前的$1變量被銷燬,之後的$2就變成了$1),而變量值提前一位。
同理,shift n後,前n位參數都會被銷燬
for var in ....; do
....
done
如:#!/bin/sh for var in A B C ; do echo "var is $var" done
4 例子:
例子1:if [ $# -lt 3 ] ; then
講解:
-e filename 如果 filename存在,則爲真
-d filename 如果 filename爲目錄,則爲真
-f filename 如果 filename爲常規文件,則爲真
-L filename 如果 filename爲符號鏈接,則爲真
-r filename 如果 filename可讀,則爲真
-w filename 如果 filename可寫,則爲真
-x filename 如果 filename可執行,則爲真
-s filename 如果文件長度不爲0,則爲真
-h filename 如果文件是軟鏈接,則爲真
filename1 -nt filename2 如果 filename1比 filename2新,則爲真。
filename1 -ot filename2 如果 filename1比 filename2舊,則爲真。
-eq 等於
-ne 不等於
-gt 大於
-ge 大於等於
-lt 小於
-le 小於等於
至於!號那就是取非了唄!
Shell的名字 $0
第一個參數 $1
第二個參數 $2
第n個參數 $n
所有參數 $@ 或 $*
參數個數 $#
例子2:
#!/bin/sh help() { cat < This is a generic command line parser demo. USAGE EXAMPLE: cmdparser -l hello -f -- -somefile1 somefile2 HELP exit 0 } while [ -n "$1" ]; do case $1 in -h) help;shift 1;; # function help is called -f) opt_f=1;shift 1;; # variable opt_f is set -l) opt_l=$2;shift 2;; # -l takes an argument -> shift by 2 --) shift;break;; # end of options -*) echo "error: no such option $1. -h for help";exit 1;; *) break;; esac done echo "opt_f is $opt_f" echo "opt_l is $opt_l" echo "first arg is $1" echo "2nd arg is $2" 您可以這樣運行該腳本: cmdparser -l hello -f -- -somefile1 somefile2 返回的結果是: opt_f is 1 opt_l is hello first arg is -somefile1 2nd arg is somefile2