linux循环之for循环

1.循环介绍

循环就是迭代(重复)一些命令的代码块,如果循环控制条件不满足的话,就结束循环

2.for循环语法

for 循环控制条件

do

commands

done

#当“循环控制条件”不满足条件,结束for循环

3.for循环样例

[root@kibana ~]# cat for-1.sh 
#!/bin/bash

for planet in "Mercury 1" "Venus 3" "Earth 5" "Mars 7" "Jupiter 9"
do
  set -- $planet
  #解析变量"planet"并且设置位置参数
  echo "$1  $2,000,000 miles from the sun"
done

exit 0
[root@kibana ~]# sh for-1.sh 
Mercury  1,000,000 miles from the sun
Venus  3,000,000 miles from the sun
Earth  5,000,000 miles from the sun
Mars  7,000,000 miles from the sun
Jupiter  9,000,000 miles from the sun
[root@kibana ~]# 
[root@kibana ~]# cat for-2.sh 
#!/bin/bash

s=0
for (( i=1; i<=100; i=i+1 ))
do
  s=$(($s+$i))
done
echo "The result of '1+2+3...+100' is  ==> $s"
[root@kibana ~]# sh for-2.sh 
The result of '1+2+3...+100' is  ==> 5050
[root@kibana ~]# 

  • 5
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小黑要上天

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值