shellScript之while语句

#!/bin/bash
int=1  #注意这里定义变量时,等号两边不能有空格
while(( "$int" <= 5))
do
    echo "$int"
    let "int++"
done

结果:

~/Note/test # ./11_while1.sh 
1
2
3
4
5

例子2:

#!/bin/bash
echo "Please input the num(1-10):"
read num
while [[ "$num" != 4 ]]
do
    if [ "$num" -lt 4 ];then
	echo "Too small,Try again"
	echo "input again"
	read num
    elif [ "$num" -gt 4 ];then
	echo "To high,Try again"
	echo "input again"
	read num
    else
	exit 0
    fi
done
echo "Congratulation,you are right."

结果:

~/Note/test # ./12while.sh 
Please input the num(1-10):
5
To high,Try again
input again
3
Too small,Try again
input again
4
Congratulation,you are right.

例子3:打印参数while

#!/bin/bash
echo "number of arguments is $#"
echo "what you input is:"

while [[ "$*" != "" ]]
do
    echo "$1"
    shift
done
结果

~/Note/test # ./14while.sh 
number of arguments is 0
what you input is:
~/Note/test # ./14while.sh 1 2 3
number of arguments is 3
what you input is:
1
2
3


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值