if 条件1 //如果条件1为真
then
命令1 //那么,执行命令1
elif 条件2 //如果条件1不成立,而条件成立
then
命令2 //那么,执行命令2
……更多的elif then组合……
else
命令2 //如果条件1、2……都不成立,那么执行命令3
fi //完成,if语句必须以单词fi终止
最简可以是形式:
if 条件
then
命令
fi
等价于
if 条件; then
命令
;可以在脚本的一行里写多条命令
例程1:
#!/bin/bash
#if test
if [ "10" -lt "12" ]
then
echo "Yes, 10 is less than 12"
else
echo "No"
fi
注意语法细节(在我的ubuntu 12.04 LTS中):
1.if或elif与[ 之间必须有一个空格,否则会出错!!!
2.条件的 [ 之后 和 ]之前也必须有一个空格
经历
因为没有注意这个细节,写的脚本会报出类似下面的错误
./cpdir.sh: 行 10: if[ 2 -ne 2 ]: 未找到命令
./cpdir.sh: 行 11: 未预期的符号 `then' 附近有语法错误
./cpdir.sh: 行 11: `then'
例子
if [ "10" -lt "12" ]在if后有空格、[后有空格、]前有空格,这就是正确的
if ["10" -lt "12"]在[后没空格、]前没空格就是错误的
if和elif后的判断条件(用 man test 查看详细内容)
[ EXPRESSION ] EXPRESSION is true
!EXPRESSION EXPRESSION is false
EXPRESSION1 -a EXPRESSION2 both EXPRESSION1 and EXPRESSION2 are true
EXPRESSION1 -o EXPRESSION2 either EXPRESSION1 or EXPRESSION2 is true
[ -n ] STRING the length of STRING is nonzero
-z STRING the length of STRING is zero
STRING1 = STRING2 the STRINGs are equal
STRING1 != STRING2 the STRINGs are not equal
INTEGER1 -eq INTEGER2 INTEGER1 is equal to INTEGER2
INTEGER1 -ge INTEGER2 INTEGER1 is greater than or equal to INTEGER2
INTEGER1 -gt INTEGER2 INTEGER1 is greater than INTEGER2
INTEGER1 -le INTEGER2 INTEGER1 is less than or equal to INTEGER2
INTEGER1 -lt INTEGER2 INTEGER1 is less than INTEGER2
INTEGER1 -ne INTEGER2 INTEGER1 is not equal to INTEGER2
FILE1 -ef FILE2 FILE1 and FILE2 have the same device and inode numbers
FILE1 -nt FILE2 FILE1 is newer(modification data) than FILE2
FILE1 -ot FILE2 FILE1 is older than FILE2
-b FILE FILE exists and is block special
-c FILE FILE exists and is character special
-d FILE FILE exists and is a directory
-e FILE FILE exists
-f FILE FILE exists and is a regular file
-g FILE FILE exists and is set-group-ID
-G FILE FILE exists and is owned by the effective group ID
-k FILE FILE exists and has its sticky bit set
-L FILE FILE exists and is a symbolic link
-O FILE FILE exists and is owned by the effective user ID
-p FILE FILE exists and is a named pipe
-r FILE FILE exists and is readable
-s FILE FILE exists and has a size greater than zero
-S FILE FILE exists and is a socket
-f [ FD ] file directory FD(stdout by default) is opened on a terminal
-u FILE FILE exists and its set-user-ID bit is set
-w FILE FILE exists and is writable
-x FILE FILE exists and is executable
例程2:
#!/bin/bash
#if test2
echo -n "Enter your name:"
read NAME
#did the user just hit return
if[ "$NAME" = "" ]
#注意字符串变量比较的具体格式
then
echo "You did not enter any information"
else
echo "Your name is ${NMAE}"
#注意字符串变量输出使用的具体格式
fi
例程3:
#!/bin/bash
#if cp test
if cp myfile.bak myfile
then
echo "good copy"
else
echo "`basename $0`:error could not copy the files" > &2
fi
例程4:
#!/bin/bash
#if elif
echo -n "Enter your name:"
read NAME
if [ -z $NAME ] || [ "$NMAE"="" ]
then
echo "You did not enter a name"
elif [ "$NMAE"="root" ]
then
echo "Hello root"
elif[ "$NAME"="perfect" ]
then
echo "Hello perfect"
else
echo "You are not root or perfect, but hi $NAME"
fi
#!/bin/bash
#breakout
while :
#while : 表示永远为真,注意while和: 之间一定要有一个空格,否则会有语法错误
do
echo -n "Enter any number[ 1...5 ]:"
read ANS
case $ANS in
1|2|3|4|5)
echo "You enter a number between 1 and 5."
;;
*)
echo "Wrong bumber, Bye."
break
#break 用于跳出while循环,而不是用于跳出case,这里不同于C等语言
;;
esac
done
#!/bin/bash
#continue
while:
do
echo -n "Enter any number [ 1...5 ]:"
read ANS
case $ANS in
1|2|3|4|5)
echo "You enter a number between 1 and 5"
;;
*)
echo -n "Wrong number, continue(y/n)?:"
read IS_CONTINUE
case $IS_CONTINUE in
y|yes|Y|YES
continue
;;
*)
break
;;
esac
esac
done