本文主要是介绍shellScript之for语句,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
#!/bin/bash
for varibale1 in 1 2 3 4 5 6
doecho "Hello,Welcome $varibale1 times"
done
#!/bin/bash
for variable1 in {1..5}
doecho "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}
dolet "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
doecho "$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++ ))
doecho "$i"
done
#!/bin/bash
for(( i = 1;i <= 100; i += 2))
dolet "sum += i"
done
echo "sum = $sum"
这篇关于shellScript之for语句的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!