shell for循环

shell for循环
 

for循环一般格式为:
for 变量 in 列表
do
  command1
  command2
  ...
  commandN
done
列表是一组值(数字、字符串等)组成的序列,每个值通过空格分隔。每循环一次,就将列表中的下一个值赋给变量。

in 列表是可选的,如果不用它,for 循环使用命令行的位置参数。

1、顺序输出当前列表中的数字

for loop in 1 2 3 4 5
do
  echo "The value is: $loop"
done
运行结果:
The value is: 1
The value is: 2
The value is: 3
The value is: 4
The value is: 5

2、顺序输出字符串中的字符:
for str in 'This is a string'
do
  echo $str
done
运行结果:
This is a string

3、显示主目录下以 .bash 开头的文件:
#!/bin/bash
for FILE in $HOME/.bash*
do
  echo $FILE
done
运行结果:
/root/.bash_history
/root/.bash_logout
/root/.bash_profile
/root/.bashrc

4、显示/usr目录下所有目录
#!/bin/bash
dir=$(ls -l /usr/ |awk '/^d/ {print $NF}')
for i in $dir
do
  echo $i
done
运行结果
bin
etc
games
include
lib
lib64
libexec
local
sbin
share
src

作者:zqifa

出处:https://www.l1mn.com

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

z_qifa

此处弱弱求打赏~~万一有好心人

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

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

打赏作者

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

抵扣说明:

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

余额充值