加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

shell for循环

发布时间:2020-12-15 07:06:08 所属栏目:安全 来源:网络整理
导读:shell for循环 for循环一般格式为: for 变量 in 列表 do ? command1 ? command2 ? ... ? commandN done 列表是一组值(数字、字符串等)组成的序列,每个值通过空格分隔。每循环一次,就将列表中的下一个值赋给变量。 in 列表是可选的,如果不用它,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

?

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读