shellScript之for语句

#!/bin/bash
for varibale1 in 1 2 3 4 5 6 
do
    echo "Hello,Welcome $varibale1 times"
done


#!/bin/bash
for variable1 in {1..5}
do
    echo "Hello,Welcome $variable1 times"
done

结果:

~/Note/test # ./6for2.sh 
Hello,Welcome 1 times
Hello,Welcome 2 times
Hello,Welcome 3 times
Hello,Welcome 4 times
Hello,Welcome 5 times

例子2:

求100内的奇数和

#!/bin/bash
for i in {1..100..2}
do
    let "sum+=i"
done
echo "The sum is $sum"
结果:

~/Note/test # ./7for.sh 
The sum is 2500

例子3:

没有列表的for语句,这种情况一般就是命令行参数给列表

#!/bin/bash
echo "number of argument is $#"
echo "what you input is:"
for arguemnt
do
    echo "$arguemnt"
done
结果:

~/Note/test # ./8for_no_list.sh 
number of argument is 0
what you input is:
~/Note/test # ./8for_no_list.sh 1 2 3 
number of argument is 3
what you input is:
1
2
3

例子4:c语言格式的for语句

#!/bin/bash
for(( i = 1; i <= 5; i++ ))
do
    echo "$i"
done

#!/bin/bash
for(( i = 1;i <= 100; i += 2))
do
    let "sum += i"
done
echo "sum = $sum"


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值