现在开始学习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跳过本次循环,没有跳出整个循环