Linux Shell之条件判断

下面介绍下shell相关的条件判断命令:

1)if-fi

if [ expression ]		#expression和方括号之间须有空格隔开,否则会有语法错误
then
	语句
fi

2)if-else-fi

if [ expression ]
then
	语句
else
	语句
fi

3)if-elif-else-fi

if [ expression1 ]
then
	语句
elif [ expression2 ]
then
	语句
elif [ expression3 ]
then
	语句
else
	语句
fi

4)if…else语句经常与test命令结合使用(test命令用于检查某个条件是否成立,与方括号的作用类似),如下所示:

num1=$[2*3]
num2=$[1+5]
if test $[num1] -eq $[num2]
then
	echo "is eqluals"
else 
	echo "isn't equals"
fi

5)case-esac
多分支选择结构。case语句匹配一个值或一个模式,如果匹配成功,执行相匹配的命令,case语法如下:

case 值 in
模式 1)
	command1
	command2
	;; #“;;”和break类似,意为跳到整个case语句的最后
模式 2)
	command1
	command2
	;;
*) #"*"号表示无匹配到的值,默认执行部分
	command1
	command2
	;;
esac

猜你喜欢

转载自blog.csdn.net/qq_32403063/article/details/89548436
今日推荐