Shell 脚本流程控制语句例子


if 语句
read -p "Enter your score(0-100): " GRADE
if [ $GRADE -ge 85 ] && [ $GRADE -le 100 ] ; then
    echo "$GRADE is excellent"
elif [ $GRADE -ge 70 ] && [ $GRADE -le 84 ] ; then
    echo "$GRADE is pass"
else
    echo "$GRADE is fail"
fi 
       echo "input invalid !!!"
for 语句
NUMBER=1
read -p "Enter The Users Password: " PASSWORD
#SAVEIFS=$IFS
#IFS=$(echo -en "\n")
for UNAME in $(cat user.txt)
do 
    id $UNAME &> /dev/null
    if [ $? -eq 0 ]
    then 
        echo "$UNAME , Already exists"
    else
        useradd $UNAME &> /dev/null
        echo "$PASSWORD" | passwd --stdin $UNAME
        echo "$UNAME $NUMBER , Creat success"
    fi
done

while 语句
PRICE=$(expr $RANDOM % 1000)
TIMES=0
echo $PRICE
echo "price range from 0 to 1000,try to guess it!"
while true
do 
    read -p "please input your anwser: " INF
    let TIMES++
    if [ "$INF" -eq "$PRICE" ]; then 
        echo "congratulations you finished the game !!!,the anwser is $PRICE"
        echo "you have tried $TIMES times."
        exit
    elif [ $INF -gt $PRICE ]; then
        echo "it is higher"
    else 
        echo "it is lower "
    fi 
done

case 语句

read -p "please input a character and press Enter to confirm: " KEY
case "$KEY" in 
    [a-z]|[A-Z])
        echo "what you input is alphabet"
        ;;
    [0-9])    echo "what you iuput is number"
        ;;
        *)
        echo "what you input is another character"
esac

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值