if 语法格式
if condition
then
Command
else
Command
fi
condition有三种表达方式
if command then if function then | 命令执行成功,等于返回0 (比如grep ,找到匹配) 执行失败,返回非0 (grep,没找到匹配) |
if [ expression_r_r_r ] then | 表达式结果为真,则返回0,if把0值引向then |
if test expression_r_r_r then | 表达式结果为假,则返回非0,if把非0值引向then |
command执行结果作为condition
if command then
if grep 'bash' test.sh; then
echo File 'test.sh' contain "\'bash\'".
else
echo File 'test.sh' not contain "\'bash\'".
fi
if
command1
command2
...
commandn
then
command
else
command
fi
if和then之间有多个command
command1, command2到commandn,
如果,所有的命令都执行成功,$?=0,执行then后的命令;(相当于command1 - commandn的执行结果的‘与’)
否则,$?=1,执行else后的命令。
注:$? 为shell中的特殊变量,通常情况下保存上一条命令执行的结果,命令执行成功$0=0,失败$0=非0。此时$?保存的是command1 - commandn的执行结果的‘与’,准确的来说应该是'或‘。所有的命令都为0,$0=0,否则$?=非零
#多条指令,这些命令之间相当于“and”(与)
read user
if
grep $user /etc/passwd >/tmp/null
who -u | grep $user
then #上边的指令都执行成功,返回值$?为0,0为真,运行then
echo "$user has logged"
else #指令执行失败,$?为1,运行else
echo "$user has not logged"
fi
函数执行结果作为condition
if function then注意:这里的function其实与command没有区别。但是由于对command来说,执行成功返回0,执行失败返回非0;因此command返回0是,if条件取到真,否则为假。
因此在实现函数时也要注意,函数得到期望的结果要return 0,这样if条件才能得到true值,否则返回非0.
function testFileExist
{
str=$1;
file=$2;
if grep "$str" "$file"; then
echo "testFileExist: File $file contain \'$str\'."
return 0 #return 0 means func exe failed
else
echo "testFileExist: File $file not contain \'$str\'."
return 1 #return !0 means func exe success
fi
}
if testFileExist 'bash' 'test.sh'; then
echo File 'test.sh' contain "\'bash\'".
else
echo File 'test.sh' not contain "\'bash\'".
fi
条件表达式作为condition
if [ condition ] then
if [ ! condition ] then
if [ condition1 -a condition2 -a condition3 -a conditionn] then
if [ condition1 -o condition2 -o condition3 -o conditionn] then
此时if条件的真假取决于实际condition的真假。
[]中可以有多个condition,使用逻辑操作符-a -o连接。-a相当于&&, -o相当于||。
#check file exist
if [ -f a.txt ]; then
echo "a.txt existed."
else
echo "a.txt not existed."
fi
#check string empty
str="hello"
if [ -z $str ]; then
echo "\$str is empty"
else
echo "\$str is not empty, str=\"$str\""
fi
#aaa.txt not exsited, then create it
if [ ! -f aaa.txt ]; then
touch aaa.txt
fi
echo "input the num:"
read num
echo "input is $num"
if [ -z "$JHHOME" -a -d $HOME/$num ]; then #如果变量$JHHOME为空,且$HOME/$num目录存在则赋值
JHHOME=$HOME/$num
echo "JHHOME is $JHHOME"
fi
if语句的快捷写法
if condition;then command fi 的快捷书写方式 [condition] && command 和 [condition] || command
[ -f "/etc/shadow" ] && echo "This computer uses shadow passwors" [ -f "/etc/shadow" ] || echo "This computer doesn't use shadow passwors" |
&& 可以理解为then 如果左边的表达式为真则执行右边的语句 || 可以理解为else 如果左边的表达式为假则执行右边的语句 |
#check file exist
[ -f a.txt ] && echo "a.txt existed."
#check string empty
str="hello"
[ -n $str ] && echo "\$str is not empty, str=\"$str\"
condition可以是直接string变量
condition可以是逻辑表达式或string变量
注:整型变量不能直接作为condition,必须写成逻辑表达式的形式。直接用整型变量作condition,条件永远为true
#string var as condition
#str2="" or don't define $str2
str2=""
if [ $str2 ]; then
echo "str2 is not empty, str2=$str2"
else
echo "str2 is empty" #go to this branch
fi
str2="hello"
if [ $str2 ]; then
echo "str2 is not empty, str2=$str2" #go to this branch
else
echo "str2 is empty"
fi
#Integer var can't to be condition directly
ivar=0
if [ $ivar ]; then
echo "ivar is not zero, ivar=$ivar" #alway go to this branch
else
echo "ivar is zero."
fi
ivar=1
if [ $ivar ]; then
echo "ivar is not zero, ivar=$ivar" #alway go to this branch
else
echo "ivar is zero."
fi
ivar=0
if [ $ivar -ne 0 ]; then #use -ne(!=)
echo "ivar is not zero, ivar=$ivar"
else
echo "ivar is zero." #go to this branch
fi
ivar=1
if [ $ivar -eq 0 ]; then #use -eq(==)
echo "ivar is zero."
else
echo "ivar is not zero, ivar=$ivar" #go to this branch
fi
各种条件表达式(condition)
if [ condition ]; then
command
else
command
fi
文件条件表达式
if [ -b file ] 当file存在并且是块文件时返回真
if [ -c file ] 当file存在并且是字符文件时返回真
if [ -f file ] 当file存在并且是正规文件时返回真
if [ -h file ] 当file存在并且是符号链接文件时返回真,该选项在一些老系统上无效
if [ -s file ] 当file存在文件大小大于0时返回真
if [ -p file ] 当file存在并且是命令管道时返回为真
if [ -g path ] 当path指定的文件或目录存在并且设置了SGID位时返回为真
if [ -r path ] 当path指定的文件或目录存在并且可读时返回为真
if [ -k path ] 当path指定的文件或目录存在并且设置了“粘滞”位时返回真
if [ -d path ] 当path存在并且是一个目录时返回真
if [ -e path ] 当path指定的文件或目录存在时返回真
if [ -u path ] 当path指定的文件或目录存在并且设置了SUID位时返回真
if [ -w path ] 当path指定的文件或目录存在并且可执行时返回真。一个目录为了它的内容被访问必然是可执行的。
if [ -o path ] 当path指定的文件或目录存在并且被子当前进程的有效用户ID所指定的用户拥有时返回真。
整型条件表达式
if [ ivar1 -eq ivar2 ] ivar1等于ivar2
if [ ivar1 -ne ivar2 ] ivar1不等于ivar2
if [ ivar1 -gt ivar2 ] ivar1大于ivar2
if [ ivar1 -lt ivar2 ] ivar1小于ivar2
if [ ivar1 -le ivar2 ] ivar1小于等于ivar2
if [ ivar1 -ge ivar2 ] ivar1大于等于ivar2
字符串条件表达式
if [ -z str ] str是空串
if [ -n str ] str是非空串
if [ str1 -eq str2 ] str1等于str2
if [ str1 -ne str2 ] str1不等于str2
if [ str1 -gt str2 ] str1大于str2
if [ str1 -lt str2 ] str1小于str2
if [ str1 -le str2 ] str1小于等于str2
if [ str1 -ge str2 ] str1大于等于str2
if [ str1 = str2 ] str1等于str2 (注意是=而不是==)
if [ str1 != str2 ] str1不等于str2
小贴士
注:=和!=只能用于字符串比较,不能用于整型.注: = 作为等于时,其两边都必须加空格,否则失效。因为等号也是操作符,必须和其他变量,关键字,用空格格开;而等号做赋值号时正好相反,两边不能有空格。
本文参考:
http://www.cnblogs.com/david-zhang-index/archive/2012/05/07/2489292.html
http://blog.chinaunix.net/uid-24607609-id-2118151.html