if then else語句
- If 條件1 //如果條件1為真
- Then //那么
- 命令1 //執行命令1
- elif 條件2 //如果條件1不成立
- then //那么
- 命令2 //執行命令2
- else //如果條件1,2均不成立
- 命令3 //那么執行命令3
- fi //完成
If 條件1 //如果條件1為真
Then //那么
命令1 //執行命令1
elif 條件2 //如果條件1不成立
then //那么
命令2 //執行命令2
else //如果條件1,2均不成立
命令3 //那么執行命令3
fi //完成
簡單的if語句
最普通的if語句是:
if條件
then 命令
if
使用if語句時,必須將then部分放在新行,否則會產生錯誤。如果要不分行,必須使用命令分隔符。本
書其余部分將采取這種形式。現在簡單 if語句變為:
if 條件;then
命令
if
- /home/l/g/tomotoboy >cat iftest
- #!/bin/sh
- #iftest
- #this is a comment line,all comment lines start with a#
- if [ "12" -lt "14" ]
- then
- #yes 12 is less than 14
- echo "Yes, 12 is less than 14"
- fi
-
- /home/l/g/tomotoboy >chmod u+x iftest
- /home/l/g/tomotoboy > ./iftest
- Yes, 12 is less than 14
/home/l/g/tomotoboy >cat iftest
#!/bin/sh
#iftest
#this is a comment line,all comment lines start with a#
if [ "12" -lt "14" ]
then
#yes 12 is less than 14
echo "Yes, 12 is less than 14"
fi
/home/l/g/tomotoboy >chmod u+x iftest
/home/l/g/tomotoboy > ./iftest
Yes, 12 is less than 14
變量值測試
不必拘泥于變量或數值測試,也可以測知系統命令是否成功返回。對 grep使用if語句找出grep是否成功
返回信息。下面的例子中 grep用于查看tomotoboy是否在數據文件sed.txt中,注意'tomotoboy'用于精
確匹配。
- /home/l/g/tomotoboy >cat grepif
- #!/bin/sh
- #grep if
- if grep 'tomotoboy' sed.txt >/dev/null 2>&1
- then
- echo "tomotoboy is in the file"
- else
- echo "tomotoboy is not in the file"
- fi
-
- /home/l/g/tomotoboy >./grepif
- tomotoboy is in the file
/home/l/g/tomotoboy >cat grepif
#!/bin/sh
#grep if
if grep 'tomotoboy' sed.txt >/dev/null 2>&1
then
echo "tomotoboy is in the file"
else
echo "tomotoboy is not in the file"
fi
/home/l/g/tomotoboy >./grepif
tomotoboy is in the file
用變量測試grep輸出
正像前面看到的,可以用grep作字符串操作。下面的腳本中,用戶輸入一個名字列表,grep在變量中查找,要求其查找指定字符串
- /home/l/g/tomotoboy >cat grepstr
- #!/bin/sh
- #grepstr
- echo -n "Enter a piece of text file:"
- read TEXT
- echo -n "Enter a string to query: "
- read QUERY
- if grep $QUERY $TEXT >/dev/null 2>&1
- then
- echo "$QUERY is in $TEXT"
- #could do some processing here...
- else
- echo "$QUERY is not in $TEXT"
- fi
-
- /home/l/g/tomotoboy >./grepstr
- -n Enter a piece of text file:
- sed.txt
- -n Enter a string to query:
- tomotoboy
- tomotoboy is in sed.txt
/home/l/g/tomotoboy >cat grepstr
#!/bin/sh
#grepstr
echo -n "Enter a piece of text file:"
read TEXT
echo -n "Enter a string to query: "
read QUERY
if grep $QUERY $TEXT >/dev/null 2>&1
then
echo "$QUERY is in $TEXT"
#could do some processing here...
else
echo "$QUERY is not in $TEXT"
fi
/home/l/g/tomotoboy >./grepstr
-n Enter a piece of text file:
sed.txt
-n Enter a string to query:
tomotoboy
tomotoboy is in sed.txt
文件拷貝輸出檢查
下面測試文件拷貝是否正常,如果 cp命令并沒有拷貝文件myfile到myfile.bak,則打印錯誤信息。注意錯誤信息中` basename $0`打印腳本名。如果腳本錯誤退出,一個好習慣是顯示腳本名并將之定向到標準錯誤中。用戶應該知道產生錯誤的腳本名。
- /home/l/g/tomotoboy >chmod u+x ifcp
- /home/l/g/tomotoboy >ifcp
- cp: cannot access myfile
- ifcp: error could not copy the file
- /home/l/g/tomotoboy >cat ifcp
- #!/bin/sh
- #ifcp
- if cp myfile myfile.bak; then
- echo "good copy"
- else
- echo "`basename $0`: error could not copy the file" >&2
- fi
- /home/l/g/tomotoboy >touch myfile
- /home/l/g/tomotoboy >ifcp
- good copy
- /home/l/g/tomotoboy >
/home/l/g/tomotoboy >chmod u+x ifcp
/home/l/g/tomotoboy >ifcp
cp: cannot access myfile
ifcp: error could not copy the file
/home/l/g/tomotoboy >cat ifcp
#!/bin/sh
#ifcp
if cp myfile myfile.bak; then
echo "good copy"
else
echo "`basename $0`: error could not copy the file" >&2
fi
/home/l/g/tomotoboy >touch myfile
/home/l/g/tomotoboy >ifcp
good copy
/home/l/g/tomotoboy >
當前目錄測試
當運行一些管理腳本時,可能要在根目錄下運行它,特別是移動某種全局文件或進行權限改變時。一個簡單的測試可以獲知是否運行在根目錄下。下面腳本中變量DIRECTORY使用當前目錄的命令替換操作,然后此變量值與 " / "字符串比較(/為根目錄) 。如果變量值與字符串不等,則用戶退出腳本,退出狀態為1意味錯誤信息產生。
- /home/l/g/tomotoboy >ifpwd
- You need to be in the root directory not /home/l/g/tomotoboy to run this script
- /home/l/g/tomotoboy >cd /etc
- /etc >cd /
- / >/home/l/g/tomotoboy/ifpwd
- / >cat ifpwd
- cat: cannot open ifpwd
- / >cat /home/l/g/tomotoboy/ifpwd
- #!/bin/sh
- #ifpwd
- DIRECTORY=`pwd`
- #grab the current directory
- if [ "$DIRECTORY" != "/" ];then
- #is it the root directory?
- #no ,the direct output to standard error,which is the screen by default.
- echo "You need to be in the root directory not $DIRECTORY to run this script" >&2
- #exit with a value of 1, an error
- exit 1
- fi
/home/l/g/tomotoboy >ifpwd
You need to be in the root directory not /home/l/g/tomotoboy to run this script
/home/l/g/tomotoboy >cd /etc
/etc >cd /
/ >/home/l/g/tomotoboy/ifpwd
/ >cat ifpwd
cat: cannot open ifpwd
/ >cat /home/l/g/tomotoboy/ifpwd
#!/bin/sh
#ifpwd
DIRECTORY=`pwd`
#grab the current directory
if [ "$DIRECTORY" != "/" ];then
#is it the root directory?
#no ,the direct output to standard error,which is the screen by default.
echo "You need to be in the root directory not $DIRECTORY to run this script" >&2
#exit with a value of 1, an error
exit 1
fi
文件權限測試
可以用i f語句測試文件權限,下面簡單測試文件sed.txt是否可寫
- /home/l/g/tomotoboy >ifwr sed.txt
- You can write to sed.txt
- /home/l/g/tomotoboy >cat ifwr
- #!/bin/sh
- #ifwr
- if [ ! -w "$1" ]; then
- echo "You cannot write to $1" >&2
- else
- echo "You can write to $1"
- fi
/home/l/g/tomotoboy >ifwr sed.txt
You can write to sed.txt
/home/l/g/tomotoboy >cat ifwr
#!/bin/sh
#ifwr
if [ ! -w "$1" ]; then
echo "You cannot write to $1" >&2
else
echo "You can write to $1"
fi
測試傳遞到腳本中的參數
if語句可用來測試傳入腳本中參數的個數。使用特定變量$#,表示調用參數的個數。可以測試所需參數個數與調用參數個數是否相等。以下測試確保腳本有三個參數。如果沒有,則返回一個可用信息到標準錯誤,然后代碼退出并顯示退出狀態。如果參數數目等于3,則顯示所有參數。
- /home/l/g/tomotoboy >cat ifparam
- #!/bin/sh
- #ifparam
- if [ $# -lt 3 ]; then
- #less than 3 parameters called,echo a usage message and exit
- echo "Usage: `basename $0` arg1 arg2 arg3" >&2
- exit
- fi
- #good ,receive 3 params, let's echo them
- echo "arg1: $1"
- echo "arg2: $2"
- echo "arg3: $3"
- /home/l/g/tomotoboy >ifparam yang shi hai
- arg1: yang
- arg2: shi
- arg3: hai
/home/l/g/tomotoboy >cat ifparam
#!/bin/sh
#ifparam
if [ $# -lt 3 ]; then
#less than 3 parameters called,echo a usage message and exit
echo "Usage: `basename $0` arg1 arg2 arg3" >&2
exit
fi
#good ,receive 3 params, let's echo them
echo "arg1: $1"
echo "arg2: $2"
echo "arg3: $3"
/home/l/g/tomotoboy >ifparam yang shi hai
arg1: yang
arg2: shi
arg3: hai
決定腳本是否為交互模式
有時需要知道腳本運行是交互模式(終端模式)還是非交互模式(cron或at) 。腳本也許需要這個信息以決定從哪里取得輸入以及輸出到哪里,使用test命令并帶有-t選項很容易確認這一點。如果test返回值為1,則為交互模式。
- /home/l/g/tomotoboy >cat ifinteractive
- #!/bin/sh
- #ifinteractive
- if [ -t ];then
- echo "We are interactive with a terminal"
- else
- echo "We must be running from some background process probably cron or at"
- fi
- /home/l/g/tomotoboy >ifinteractive
- We are interactive with a terminal
/home/l/g/tomotoboy >cat ifinteractive
#!/bin/sh
#ifinteractive
if [ -t ];then
echo "We are interactive with a terminal"
else
echo "We must be running from some background process probably cron or at"
fi
/home/l/g/tomotoboy >ifinteractive
We are interactive with a terminal
簡單的if else語句
下一個if語句有可能是使用最廣泛的:
- if條件
- then
- 命令1
- else
- 命令2
- fi
if條件
then
命令1
else
命令2
fi
使用if語句的else部分可在條件測試為假時采取適當動作。
變量設置測試,下面的例子測試環境變量EDITOR是否已設置。如果EDITOR變量為空,將此信息通知用戶。如果已設置,在屏幕上顯示編輯類型。
- /home/l/g/tomotoboy >echo $EDITOR
-
- /home/l/g/tomotoboy >cat ifeditor
- #!/bin/sh
- #ifeditor
- if [ -z "$EDITOR" ];then
- #the variable has not been set
- echo "Your EDITOR environment is not set"
- else
- #let's us see what is it
- echo "Using $EDITOR as the default editor"
- fi
- /home/l/g/tomotoboy >ifeditor
- Your EDITOR environment is not set
- /home/l/g/tomotoboy >
/home/l/g/tomotoboy >echo $EDITOR
/home/l/g/tomotoboy >cat ifeditor
#!/bin/sh
#ifeditor
if [ -z "$EDITOR" ];then
#the variable has not been set
echo "Your EDITOR environment is not set"
else
#let's us see what is it
echo "Using $EDITOR as the default editor"
fi
/home/l/g/tomotoboy >ifeditor
Your EDITOR environment is not set
/home/l/g/tomotoboy >
檢測最后命令狀態
前面將目錄名傳入腳本創建了一個目錄,腳本然后提示用戶是否應創建目錄。下面的例子創建一個目錄,并從當前目錄將所有 *.txt文件拷入新目錄。但是這段腳本中用最后狀態命令檢測了每一個腳本是否成功執行。如果命令失敗則通知用戶。
- /home/l/g/tomotoboy >cat ifmkdir
- #!/bin/sh
- #ifmkdir
- DIR_NAME=testdirec
- #where we are?
- THERE=`pwd`
- #send all output to system dustbin
- mkdir $DIR_NAME >/dev/null 2>&1
- #is it a directory?
- if [ -d $DIR_NAME ];then
- #can we cd to the directory
- cd $DIR_NAME
- if [ $? = 0 ];then
- #yes we can
- HERE=`pwd`
- echo "$HERE"
- cp $THERE/*.txt $HERE
- else
- echo "Cannot cd to $DIR_NAME" >&2
- exit 1
- fi
- else
- echo "Cannot create directory $DIR_NAME" >&2
- exit 1
- fi
/home/l/g/tomotoboy >cat ifmkdir
#!/bin/sh
#ifmkdir
DIR_NAME=testdirec
#where we are?
THERE=`pwd`
#send all output to system dustbin
mkdir $DIR_NAME >/dev/null 2>&1
#is it a directory?
if [ -d $DIR_NAME ];then
#can we cd to the directory
cd $DIR_NAME
if [ $? = 0 ];then
#yes we can
HERE=`pwd`
echo "$HERE"
cp $THERE/*.txt $HERE
else
echo "Cannot cd to $DIR_NAME" >&2
exit 1
fi
else
echo "Cannot create directory $DIR_NAME" >&2
exit 1
fi
簡單的安全登錄腳本
以下是用戶登錄時啟動應用前加入相應安全限制功能的基本框架。首先提示輸入用戶名和密碼,如果用戶名和密碼均匹配腳本中相應字符串,用戶登錄成功,否則用戶退出。腳本首先設置變量為假—總是假定用戶輸入錯誤,stty當前設置被保存,以便隱藏passwd域中字符,然后重新保存stty設置。如果用戶I D和密碼正確(密碼是myday) ,明亮INVALID_USER和INVALID_PASSWD設置為no表示有效用戶或密碼,然后執行測試,如果兩個變量其中之一為yes,缺省情況下,腳本退出用戶。鍵入有效的ID和密碼,用戶將允許進入。這是一種登錄腳本的基本框架。下面的例子中有效用戶ID為dave或tomotoboy。
- #!/bin/sh
- #ifpass
- #set the variables to false
- INVALID_USER=yes
- INVALID_PASSWD=yes
- #set the current stty settings
- SAVEDSTTY=`stty -g`
- echo "You are logging into a sensitive area"
- echo -n "Enter your ID name:"
- read NAME
- #hide the characters typed in
- stty -echo
- echo "Enter your password :"
- read PASSWORD
- #back on again
- stty $SAVEDSTTY
- if [ "$NAME" = "tomotoboy" ] || [ "$NAME" = "dave" ]; then
- #if a valid then set variable
- INVALID_USER=no
- fi
- if [ "$PASSWORD" = "myday" ];then
- #if valid password then set variable
- INVALID_PASSWD=no
- fi
- if [ "$INVALID_USER" = "yes" ] || [ "$INVALID_PASSWD" = "yes" ];then
- echo "`basename $0` : Sorry wrong password or userid"
- exit 1
- fi
- echo "corrent user id and password given"
#!/bin/sh
#ifpass
#set the variables to false
INVALID_USER=yes
INVALID_PASSWD=yes
#set the current stty settings
SAVEDSTTY=`stty -g`
echo "You are logging into a sensitive area"
echo -n "Enter your ID name:"
read NAME
#hide the characters typed in
stty -echo
echo "Enter your password :"
read PASSWORD
#back on again
stty $SAVEDSTTY
if [ "$NAME" = "tomotoboy" ] || [ "$NAME" = "dave" ]; then
#if a valid then set variable
INVALID_USER=no
fi
if [ "$PASSWORD" = "myday" ];then
#if valid password then set variable
INVALID_PASSWD=no
fi
if [ "$INVALID_USER" = "yes" ] || [ "$INVALID_PASSWD" = "yes" ];then
echo "`basename $0` : Sorry wrong password or userid"
exit 1
fi
echo "corrent user id and password given"