shell条件判断语句用法(shell流程控制语句之if语句)

发布时间:2020-10-21编辑:脚本学堂
有关shell if语句的用法,shell条件判断语句之if语句用法,并介绍了shell case语句、while循环语句、for循环语句以及select语句的几个例子。

shell if语句

知识:

[ -f “somefile” ] :判断是否是一个文件

[ -x “/bin/ls” ] :判断/bin/ls是否存在并有可执行权限

[ -n “$var” ] :判断$var变量是否有值

[ “$a” = “$b” ] :判断$a和$b是否相等

例子(shell条件判断语句之if语句用法):
 

#!/bin/sh
varOne=1
varTwo=2
varThree=3
if [ "$varOne" = "$varTwo" ]; then
echo "varTwo:$varTwo"
elif [ "$varOne" = "$varThree" ]; then
echo "varThree:$varThree"
else
echo "varOne:$varOne"
fi
 

注意,[]比较的时候其括号前后的空格别忘了! = 等号前后也要有空格也要注意;

&& 和 || 操作符

例子:
 

#!/bin/sh
varOne=1
varTwo=2
varThree=3
if [ "$varOne" = "$varThree" ] || [ "$varOne" = "$varTwo" ]; then
echo "|| 进入"
else
echo "No || 进入"
fi

if [ "$varOne" = "$varOne" ] && [ "$varOne" = "$varTwo" ]; then
echo "&& 进入"
else
echo "No && 进入"
fi
 

case 语句

注意:

case表达式可以用来匹配一个给定的字符串,而不是数字(可别和C语言里的switch…case混淆)。

例子:
 

#!/bin/sh

ftype=`file
"$1"`   # Note ' and ` is different
case "$ftype" in
"$1: Zip archive"*)
unzip "$1" ;;
"$1: gzip compressed"*)
gunzip "$1" ;;
"$1: bzip2 compressed"*)
bunzip2 "$1" ;;
*) echo "File $1 can not be uncompressed with smartzip";;
esac
 

特殊变量$1,该变量包含有传递给该脚本的第一个参数值,也就是说,$1 就是字符串 articles.zip。

select 语句

select表达式是bash的一种扩展应用,擅长于交互式场合。用户可以从一组不同的值中进行选择:

select var in ...
; do
 break;
done
.... now $var can be used ....

例子:
 

#!/bin/sh

echo "What is your favourite OS?"
select var in "linux" "Gnu Hurd" "Free BSD" "Other"; do
break;
done
echo "You have selected $var"
 

如果 以上脚本运行出现 select :NOT FOUND 将 #!/bin/sh 改为 #!/bin/bash 该脚本的运行结果如下:
What is your favourite OS?
1) Linux
2) Gnu Hurd
3) Free BSD
4) Other
#? 1
You have selected Linux

while/for 循环

例子:
 

复制代码 代码示例:

#!/bin/sh
varOne=1
varTwo=1

# while
while [ "$varOne" = "$varOne" ]; do
echo "while Done"
break
done

#  for
for varStr in H I M I ; do
echo "varStr is $varStr"
done

输出:

while Done
varStr is H
varStr is I
varStr is M
varStr is I
localhost:Desktop Himi$

select 语句

注意:select表达式是bash的一种扩展应用,擅长于交互式场合。用户可以从一组不同的值中进行选择:

例子:
 

#!/bin/sh
echo "What is your favourite?"
select var in "iOS" "Android" "Himi" "Other"; do
break;
done
echo "You have selected $var"
 

执行脚本后,等待用户输入,然后在终端输入你的选择,回车,如下显示:
What is your favourite?
1) iOS
2) Android
3) Himi
4) Other
#? 3
You have selected Himi
localhost:Desktop Himi$

函数:
在几个地方使用了相同的代码,这时如果用上函数,会方便很多。

函数的大致样子:

functionname()
{
# inside the body $1 is the first argument given to the function
# $2 the second ...
body
}

例子:
 

#!/bin/sh
himi()
{
echo "Function is ok"
exit 0
}
himi

脚本调试:

最简单的调试方法当然是使用echo命令。你可以在任何怀疑出错的地方用echo打印变量值,这也是大部分shell程序员花费80%的时间用于调试的原因。shell脚本的好处在于无需重新编译,而插入一个echo命令也不需要多少时间。shell也有一个真正的调试模式,如果脚本”strangescript”出错,可以使用如下命令进行调试:

sh -x strangescript

上述命令会执行该脚本,同时显示所有变量的值。shell还有一个不执行脚本只检查语法的模式,命令如下:

sh -n your_script   


shell if...elif...fi 语句

if...elif...fi 语句是提前一个级别的控制语句,形式出几个条件,允许 Shell 作出正确的决定。

语法:
 

if [ expression 1 ]
then
Statement(s) to be executed if expression 1 is true
elif [ expression 2 ]
then
Statement(s) to be executed if expression 2 is true
elif [ expression 3 ]
then
Statement(s) to be executed if expression 3 is true
else
Statement(s) to be executed if no expression is true
fi

例子,如果条件不为 ture,则执行else块。
 

#!/bin/sh

a=10
b=20

if [ $a == $b ]
then
echo "a is equal to b"
elif [ $a -gt $b ]
then
echo "a is greater than b"
elif [ $a -lt $b ]
then
echo "a is less than b"
else
echo "None of the condition met"
fi

结果:
a is less than b