[b]if语句使用[/b]
Shell 有三种 if ... else 语句:
case语句使用
语法:
执行的脚本
运行后的结果sh ifelse.sh -f "test/"
Shell 有三种 if ... else 语句:
if ... fi 语句;
if ... else ... fi 语句;
if ... elif ... else ... fi 语句。case语句使用
语法:
case 值 in
模式1)
command1
;;
模式2)
command1
;;
*)
command1
;;
esac执行的脚本
a=10
b=20
if [ $a == $b ]
then
printf "%s %s %s" a=10 "和" b=20 "相等"
elif [ $a != $b ]
then
printf "%s%s%s\n" "a=10" "和" "b=20" "不相等"
fi
#if 和test配合使用
number1=$[2*3]
number2=$[1+3]
if test $[number1] -eq $[number2]
then
echo "number1 == number2"
else
echo "number1 !=number2"
fi
#使用case
echo "输入一个数字"
read num
case $num in
1) echo "输出1"
;;
2) echo "输出2"
;;
*) echo "没有您要的输出值"
exit
;;
esac
#实际中的file操作指定是file还是dir,才会运行操作
#具体条件看之前的文档http://janle.iteye.com/blog/2367784
option=${1}
case ${option} in
-f) FILE="${2}"
echo "FILE name is $FILE"
;;
-d) DIR="${2}"
echo "DIR name is $DIR"
;;
*) echo "`basename ${0}`:使用:[-f file] | [-d directory]"
exit
;;
esac运行后的结果sh ifelse.sh -f "test/"
a=10和b=20
不相等
number1 !=number2
输入一个数字
2
输出2
FILE name is test/
本文详细介绍了Shell脚本中if...else及case语句的使用方法,并通过实例演示了如何结合test命令进行条件判断,包括数值比较、文件类型判断等常见应用场景。
1050

被折叠的 条评论
为什么被折叠?



