shell 分支循环

//test1.sh
#!/bin/sh

if [ -d test_test ]; then
        echo "It's a dir"
elif [ -f test_test ]; then
        echo "It's a file"
else
        echo "hehe"
fi

#!/bin/sh

if [ -d test_test ]
then
        echo "test_test is dir"
elif [ -f test_test ]
then
        echo "test_test id file"
else
        echo "hehe"
fi


//test2.sh
#!/bin/sh

echo "Is it morning?please answer yes or no."
read YES_OR_NO
if [ $YES_OR_NO = "yes" ]
then
        echo "good morning"
elif [ $YES_OR_NO = "no" ]
then
        echo "good afternoon"
else
        echo "sorry,$YES_OR_NO not recognized .Enter yes or no."
        exit 1
fi
exit 0

//case 

#!/bin/sh

echo "please input yes_or_no."
read YES_OR_NO

case "$YES_OR_NO" in
        "yes")
                echo "echo yes";;
        "no")
                echo "echo no";;
        *)
                echo "break"
                exit 1;;
esac
exit 0;


循环

//for
#!/bin/sh

for FRUIT in apple banana pear;do
        echo "I like $FRUIT"
done



#!/bin/sh

for filename in $(ls);do
        if [ -d $filename ];then
                echo "$filename is dir"
        elif [ -f $filename ];then
                echo "$filename is file"
        else
                echo "$file is hehe"
        fi
done



//while
#!/bin/sh

echo "please input passwd"
read passwd
while [ $passwd != "passwd" ];do
        echo "passwd is error"
        read passwd
done


#!/bin/sh
count=1
echo "please input passwd"
read passwd
while [ $passwd != "passwd" -a $count -lt 2 ];do
        echo "passwd is error"
        count=$((count+1))
        echo $count
        read passwd
done

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值