shell学习——分支、循环

现在开始学习shell基础知识——分支语句

资料来源:黑马人工智能

①if/then/elif/else/fi

if [ -f /bin/bash ];then  //then换行的话,分号不要

  echo "it's a file"

fi

if [ -f /bin/bash ]

then echo "it's a file"

else echo "it's not a file"

fi

echo "Enter yes or no..."

read YES_OR_NO

if [ "$YES_OR_NO" = "yes"];then

    echo "It's morning!"

elif ["$YES_OR_NO" = "no"];then

    echo "It's afternoon!" 

else

    echo "sorry"

    exit 1

fi

exit 0

 

②case/esac

echo "It it morning? Enter yes or no..."

read YES_OR_NO

case "$YES_OR_NO" in

yes|y|Yes|YES)

--echo "Good morning!";;

[nN]*)

    echo "Good afternoon!";;

*)

    echo "sorry!"

    exit 1;;

esac

exit 0

③插上一句:linux的服务启动脚本在/etc/init.d目录下

④for/do/done

for $FRUIT in apple orange banana;do
    echo "I like $FRUIT"
done
echo "Enter your password..."
read PASSWORD
while [ $PASSWORD != "123456" ];do
    echo "WRONG,TRY AGIAN..."
    read PASSWORD
done

⑤break/continue

#! /bin/sh

echo "Enter your password... 3 times allowed."
COUNT=0
read PASSWORD
while [ $PASSWORD != "123456" ];do
    echo "WRONG,TRY AGIAN..."
    COUNT=$(($COUNT+1))
    echo $COUNT
    if [ "$COUNT" = 3 ];then
        echo "Sorry 3 times over!"
        break
    fi
    read PASSWORD
done

break[n] n可以指定跳出几层循环,continue跳过本次循环,没有跳出整个循环

 

转载于:https://www.cnblogs.com/lgzy/p/8961117.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值