bash循环

bash中有for、while、until三种循环结构;for循环次数一般是固定的,while和until的循环次数可以是固定也可以是不固定的。1、for循环:for循环次数是固定的1.1、语法结构:(1)、for 变量名 [ in 取值列表]do command1; command2; ...doneor 可以写到一行:for 变量名 [ in 取值列表 ] ;...
摘要由CSDN通过智能技术生成
   bash中有for、while、until三种循环结构;for循环次数一般是固定的,while和until的循环次数可以是固定也可以是不固定的。

1、for循环:

for循环次数是固定的

1.1、语法结构:
(1)、
for 变量名 [ in 取值列表]
do
	command1;
	command2;
	...
done

or 可以写到一行:
for 变量名 [ in 取值列表 ] ; do command1; command2... done;

(2)、
for ((初值;条件;不长))
do
	command1;
	command2;
	...
done
1.2、实例:

seq 命令: 生成数字序列

[root@centos7 scripts]# cat for_sum.sh 
#!/usr/bin/bash
#sum求和
#20191210 by zhaoyq

for i in {
   1..100}
do
	let sum+=$i
done

echo "sum= $sum"


[root@centos7 scripts]# 
[root@centos7 scripts]# 
[root@centos7 scripts]# 
[root@centos7 scripts]# ./for_sum.sh 
sum= 5050
[root@centos7 scripts]#
[root@centos7 scripts]# cat for_sum2.sh 
#!/usr/bin/bash
#sum求和 
#seq 序列
#v1.0 20191210 by zhaoyq

for i in `seq $1`
do
	let sum+=$i
done

echo "sum= $sum"


[root@centos7 scripts]# 
[root@centos7 scripts]# ./for_sum2.sh 100
sum= 5050
[root@centos7 scripts]#
[root@centos7 scripts]# cat user.txt 
zhangsan
lisi
wangwu
zhaoliu

[root@centos7 scripts]# 
[root@centos7 scripts]# cat read_for.sh 
#!/usr/bin/bash
#按行循环读取文件内容
#v1.0 by zhaoyq 20191210

for user in `cat user.txt`
do
	echo $user
done
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值