shell脚本的简单使用:五—— 之逻辑判断

[b]if语句使用[/b]
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/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值