Linux shell编程学习笔记17:for循环语句

Linux Shell 脚本编程和其他编程语言一样,支持算数、关系、布尔、字符串、文件测试等多种运算,同样也需要进行根据条件进行流程控制,提供了if、for、while、until等语句。

之前我们探讨了if语句,现在我们来探讨for循环语句。

Linux Shell中的for语句十分灵活,格式多样,我们通过实例看看一些常用的格式。

一、数字条件类循环

(一)格式1:C语言风格

csdn @ edu in ~ [22:14:49]

$ ++for (( i=1; i <=10; i++)); do++

for>++echo "$i * 2 = $(expr $i \* 2)";++

for>++done++

1 * 2 = 2

2 * 2 = 4

3 * 2 = 6

4 * 2 = 8

5 * 2 = 10

6 * 2 = 12

7 * 2 = 14

8 * 2 = 16

9 * 2 = 18

10 * 2 = 20

csdn @ edu in ~ [22:15:25]

$

(二)格式2:in + 完整数列

1.在zsh中

csdn @ edu in ~ [23:22:07] C:127

$ ++for i in 1 2 3 4 5 6 7 8 9 10; do++

for> ++echo "$i * 2 = $(expr $i \* 2)";++

for> ++done++

1 * 2 = 2

2 * 2 = 4

3 * 2 = 6

4 * 2 = 8

5 * 2 = 10

6 * 2 = 12

7 * 2 = 14

8 * 2 = 16

9 * 2 = 18

10 * 2 = 20

csdn @ edu in ~ [23:22:44]

$

2.在bash中

[csdn ~]$ ++for i in 1 2 3 4 5 6 7 8 9 10; do echo "$i * 2 = $(expr $i \* 2)"; done++

1 * 2 = 2

2 * 2 = 4

3 * 2 = 6

4 * 2 = 8

5 * 2 = 10

6 * 2 = 12

7 * 2 = 14

8 * 2 = 16

9 * 2 = 18

10 * 2 = 20

[csdn ~]$

(三)格式3:使用 in + seq集合

csdn @ edu in ~ [23:27:05]

$ ++for i in $(seq 1 10) ; do++

for> ++echo "$i * 2 = $(expr $i \* 2)";++

for> ++done++

1 * 2 = 2

2 * 2 = 4

3 * 2 = 6

4 * 2 = 8

5 * 2 = 10

6 * 2 = 12

7 * 2 = 14

8 * 2 = 16

9 * 2 = 18

10 * 2 = 20

csdn @ edu in ~ [23:28:42]

$

(四)格式4:用 in + {..}集合

1.多余空格导致错误

csdn @ edu in ~ [23:36:02]

$ ++for i in {1..10}; do++

zsh: command not found: do

zsh: command not found: do

zsh: command not found: do

zsh: command not found: do

zsh: command not found: do

zsh: command not found: do

zsh: command not found: do

zsh: command not found: do

zsh: command not found: do

zsh: command not found: do

导致出错的原因是命令

++for i in {1..10}; do++

中的 }; 和行末的 do 之间多放了几个空格,只需要一个空格就行。

2.正确的格式

csdn @ edu in ~ [23:36:09] C:127

$ ++for i in {1..10}; do++

for> ++echo "$i * 2 = $(expr $i \* 2)";++

for> ++done++

1 * 2 = 2

2 * 2 = 4

3 * 2 = 6

4 * 2 = 8

5 * 2 = 10

6 * 2 = 12

7 * 2 = 14

8 * 2 = 16

9 * 2 = 18

10 * 2 = 20

csdn @ edu in ~ [23:36:47]

$

(五)格式5:在awk中应用

csdn @ edu in ~ [22:41:42]

$ ++awk 'BEGIN {for( i=1; i<=10; i++ ) print i "*2=" i*2}'++

1*2=2

2*2=4

3*2=6

4*2=8

5*2=10

6*2=12

7*2=14

8*2=16

9*2=18

10*2=20

csdn @ edu in ~ [22:41:57]

$

二、字符条件类循环

(一)格式1:in 字符串

1.在bash中有效

[csdn ~]$ ++s="b d 3";for i in $s; do if [[ $i > 'a' && $i < 'z' ]]; then echo $i is a lowercase letter; else echo $i is not a letter; fi; done++

b is a lowercase letter

d is a lowercase letter

3 is not a letter

[csdn ~]$

2.在zsh中无效

[csdn ~]$ exec zsh

csdn @ edu in ~ [14:00:44]

$ ++s="b d 3";for i in $s; do if [[ $i > 'a' && $i < 'z' ]]; then echo $i is a lowercase letter; else echo $i is not a letter; fi; done++

b d 3 is a lowercase letter

csdn @ edu in ~ [14:00:55]

$

(二)格式1:in 字符1 字符2 ...... 字符n

1.在bash中部分有效

csdn @ edu in ~ [14:26:43]

$ ++exec bash++

[csdn ~]$ ++for i in 'b' 'd' '3'; do if [[ $i > 'a' && $i < 'z' ]]; then echo $i is a lowercase letter; else echo $i is not a letter; fi; done++

b is a lowercase letter

d is a lowercase letter

3 is not a letter

[csdn ~]$ ++for i in b d 3; do if [[ $i > 'a' && $i < 'z' ]]; then echo $i is a lowercase letter; else echo $i is not a letter; fi; done++

>

> ++^C++

[csdn ~]++$ for i in 'b d 3'; do if [[ $i > 'a' && $i < 'z' ]]; then echo $i is a lowercase letter; else echo $i is not a letter; fi; done ;++

> ++^C++

[csdn ~]$ ++for i in "b d 3"; do if [[ $i > 'a' && $i < 'z' ]]; then echo $i is a lowercase letter; else echo $i is not a letter; fi; done ;++

> ++^C++

[csdn ~]$ ++for i in "b d 3"; do if [[ $i > 'a' && $i < 'z' ]]; then echo $i is a lowercase letter; else echo $i is not a letter; fi; done++

> ++^C++

[csdn ~]$

2.在zsh中有效

csdn @ edu in ~ [14:24:52]

$ ++for i in 'b' 'd' '3'; do if [[ $i > 'a' && $i < 'z' ]]; then echo $i is a lowercase letter; else echo $i is not a letter; fi; done++

b is a lowercase letter

d is a lowercase letter

3 is not a letter

csdn @ edu in ~ [14:25:11]

$ ++for i in b d 3; do if [[ $i > 'a' && $i < 'z' ]]; then echo $i is a lowercase letter; else echo $i is not a letter; fi; done++

b is a lowercase letter

d is a lowercase letter

3 is not a letter

csdn @ edu in ~ [14:26:43]

$

(三)格式3:in {..}

1.在bash中有效

[csdn ~]$ for i in {b..d}; do

> ++if [[ $i > 'a' && $i < 'z' ]]; then++

> ++echo "$i is a letter";++

> ++else++

> ++echo "$i is not a letter";++

> ++fi;++

> ++done++

b is a letter

c is a letter

d is a letter

[csdn ~]$

或者

for i in {b..d}; do if [[ $i > 'a' && i \< 'z' \]\]; then echo "i is a letter"; else echo "$i is not a letter"; fi; done

这条命令在csdn的linux学习环境中显示如下:

2.在zsh中无效

[csdn ~]$ ++exec zsh++

csdn @ edu in ~ [21:58:49]

$ ++for i in {b..d}; do++

for> ++if [[ $i > 'a' && $i < 'z' ]]; then++

for then> ++echo "$i is a letter";++

for then>++else++

for else> ++echo "$i is not a letter";++

for else> ++fi;++

for> ++done++

{b..d} is not a letter

csdn @ edu in ~ [21:59:03]

$

(四) in [ 字母1,字母n ]

注意: [后面和] 的前面加了空格

1.在bash中

[csdn ~]$ for i in [ b,d ]; do

> if [[ $i > 'a' && $i < 'z' ]]; then

> echo "$i is a letter";

> else

> echo "$i is not a letter";

> fi;

> done

[ is not a letter

b,d is a letter

] is not a letter

[csdn ~]$

2.在zsh中

csdn @ edu in ~ [23:08:28]

$ ++for i in [ b,d ]; do++

for>++if [[ $i > 'a' && $i < 'z' ]]; then++

for then>++echo "$i is a letter";++

for then>++else++

for else>++echo "$i is not a letter";++

for else>++fi;++

for>++done++

[ is not a letter

b,d is a letter

] is not a letter

csdn @ edu in ~ [23:12:14]

$

(五) in [ 字母1 , 字母n ]

注意:[ 字母1 , 字母n ] 之间均有空格间隔。

1.在bash中

[csdn ~]$ for i in [ b , d ]; do

> if [[ $i > 'a' && $i < 'z' ]]; then

> echo "$i is a letter";

> else

> echo "$i is not a letter";

> fi;

> done

[ is not a letter

b is a letter

, is not a letter

d is a letter

] is not a letter

[csdn ~]$

2.在zsh中

[csdn ~]$ ++exec zsh++

csdn @ edu in ~ [23:08:21]

$ ++for i in [ b , d ]; do++

for> ++if [[ $i > 'a' && $i < 'z' ]]; then++

for then>++echo "$i is a letter";++

for then>++else++

for else>++echo "$i is not a letter";++

for else>++fi;++

for>++done++

[ is not a letter

b is a letter

, is not a letter

d is a letter

] is not a letter

(六)一些无效的格式

1.in 字母1-字母n

(1)在basth中

csdn @ edu in ~ [22:36:03]

$ ++exec bash++

[csdn ~]$ ++for i in b-d; do++

>++if [[ $i > 'a' && $i < 'z' ]]; then++

> ++echo "$i is a letter";++

> ++else++

>++echo "$i is not a letter";++

>++fi;++

>++done++

b-d is a letter

[csdn ~]$

(2)在zsh中

csdn @ edu in ~ [22:01:51]

$ for i in b-d; do

if [[ $i > 'a' && $i < 'z' ]]; then

echo "$i is a letter";

else

echo "$i is not a letter";

fi;

done

b-d is a letter

csdn @ edu in ~ [22:02:09]

$

2. in [字母1-字母n]

(1)在bash中

[csdn ~]$ ++for i in [b-d]; do++

> ++if [[ $i > 'a' && $i < 'z' ]]; then++

>++echo "$i is a letter";++

> ++else++

> ++echo "$i is not a letter";++

> ++fi;++

> ++done++

[b-d] is a letter

(2)在zsh中

csdn @ edu in ~ [22:25:27]

$ ++for i in [b-d]; do++

for>++if [[ $i > 'a' && $i < 'z' ]]; then++

for then>++echo "$i is a letter";++

for then> else

for else> ++echo "$i is not a letter";++

for else> ++fi;++

for>++done++

zsh: no matches found: [b-d]

csdn @ edu in ~ [22:28:11]

$

3. in [字母1,字母n]

(1)在bash中

[csdn ~]$ for i in [b,d]; do

> if [[ $i > 'a' && $i < 'z' ]]; then

> echo "$i is a letter";

> else

> echo "$i is not a letter";

> fi;

> done

[b,d] is a letter

[csdn ~]$

(2)在zsh中

csdn @ edu in ~ [22:29:14]

$++for i in [b,d]; do++

for>++if [[ $i > 'a' && $i < 'z' ]]; then++

for then> ++echo "$i is a letter";++

for then>++else++

for else> ++echo "$i is not a letter";++

for else>++fi;++

for>++done++

zsh: no matches found: [b,d]

csdn @ edu in ~ [22:33:29]

$

4. in '字母1-字母n'

(1)在bash中

[csdn ~]$ ++for i in 'b-d'; do++

>++if [[ $i > 'a' && $i < 'z' ]]; then++

> ++echo "$i is a letter";++

> ++else++

>++echo "$i is not a letter";++

>++fi;++

>++done++

b-d is a letter

[csdn ~]$

(2)在zsh中

csdn @ edu in ~ [22:33:29]

$++for i in 'b-d'; do++

for>++if [[ $i > 'a' && $i < 'z' ]]; then++

for then>++echo "$i is a letter";++

for then>++else++

for else>++echo "$i is not a letter";++

for else>++fi;++

for>++done++

b-d is a letter

csdn @ edu in ~ [22:36:03]

$

5. in '字母1'-'字母n'

(1)在bash中

[csdn ~]$ ++for i in 'b'-'d'; do++

> ++if [[ $i > 'a' && $i < 'z' ]]; then++

>++echo "$i is a letter";++

> ++else++

> ++echo "$i is not a letter";++

>++fi;++

>++done++

b-d is a letter

[csdn ~]$

(2)在zsh中

csdn @ edu in ~ [22:46:27]

$ ++for i in 'b'-'d'; do++

for> ++if [[ $i > 'a' && $i < 'z' ]]; then++

for then> ++echo "$i is a letter";++

for then>++else++

for else>++echo "$i is not a letter";++

for else> ++fi;++

for>++done++

b-d is a letter

csdn @ edu in ~ [22:46:35]

$

6.in ['字母1'-'字母n']

(1)在bash中

csdn @ edu in ~ [22:51:56]

$ ++exec bash++

[csdn ~]$ ++for i in ['b'-'d']; do++

>++if [[ $i > 'a' && $i < 'z' ]]; then++

>++echo "$i is a letter";++

>++else++

> ++echo "$i is not a letter";++

>++fi;++

>++done++

[b-d] is a letter

[csdn ~]$

(2)在zsh中

csdn @ edu in ~ [22:51:06]

$ ++for i in ['b'-'d']; do++

for>++if [[ $i > 'a' && $i < 'z' ]]; then++

for then>++echo "$i is a letter";++

for then>++else++

for else>++echo "$i is not a letter";++

for else>++fi;++

for>++done++

zsh: no matches found: [b-d]

csdn @ edu in ~ [22:51:56]

$

三、文件和目录类循环操作

(一)实例1:列出根目录/下的内容,判断其是文件还是目录并输出

csdn @ edu in ~ [11:39:57] C:1

$ ++cd /++

csdn @ edu in / [11:40:01]

$ ++ls++

anaconda-post.log bin dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var

csdn @ edu in / [11:40:04]

$ ++for i in `ls`; do if [ -f $i ]; then echo $i is a file\! ; elif [ -d $i ]; then echo $i is a directory\! ; else echo $i is other file\!; fi; done++

anaconda-post.log is a file!

bin is a directory!

dev is a directory!

etc is a directory!

home is a directory!

lib is a directory!

lib64 is a directory!

media is a directory!

mnt is a directory!

opt is a directory!

proc is a directory!

root is a directory!

run is a directory!

sbin is a directory!

srv is a directory!

sys is a directory!

tmp is a directory!

usr is a directory!

var is a directory!

csdn @ edu in / [11:43:15]

$

我们使用 cd / 命令让根目录/变成当前目录,然后用引用符(Esc键下面的`) 将ls命令获取的根目录/内容作为循环变量用if语句进行判断处理:

用 -f判断是否为文件,如果是文件就输出文件名称并注明is a file

否则用 -d判断是否为目录 ,如果是目录就输出目录名称并注明is a directory

否则就输出文件名称并注明is other file

需要注意的是,我们用引用符(Esc键下面的`) 来引有ls命令获取的根目录/内容。

(二)列出当前目录下的.sh文件名并输出提示is a shell script file !

csdn @ edu in ~ [14:03:58]

$ ++ls *.sh++

zsh: no matches found: *.sh

csdn @ edu in ~ [14:05:37] C:1

$ ++echo "hello" > hello.sh++

csdn @ edu in ~ [14:05:55]

$ ++cat hello.sh++

hello

csdn @ edu in ~ [14:06:02]

$ ++for f in $(ls *.sh);do echo $f is a shell script file \! ; done++

hello.sh is a shell script file !

csdn @ edu in ~ [14:07:16]

$

在上面的实例中,我们先用ls命令检查,发现当前目录下没有.sh文件

然后 我们用输出重定向创建了一个hello.sh

接着我们用for语句列出当前目录下的.sh文件名,并输出提示is a shell script file !

这里我们同样是引入了ls命令执行的结果,但引用的方法跟 (一)实例1:列出根目录/下的内容,判断其是文件还是目录并输出里不同,大家可以对比一下这两种方法 。

相关推荐
一坨阿亮3 分钟前
Linux 使用中的问题
linux·运维
dsywws1 小时前
Linux学习笔记之vim入门
linux·笔记·学习
幺零九零零2 小时前
【C++】socket套接字编程
linux·服务器·网络·c++
小林熬夜学编程3 小时前
【Linux系统编程】第四十一弹---线程深度解析:从地址空间到多线程实践
linux·c语言·开发语言·c++·算法
程思扬4 小时前
为什么Uptime+Kuma本地部署与远程使用是网站监控新选择?
linux·服务器·网络·经验分享·后端·网络协议·1024程序员节
sun0077004 小时前
拷贝 cp -rdp 和 cp -a
linux·运维·服务器
wowocpp4 小时前
ubuntu 22.04 server 安装 anaconda3
linux·运维·ubuntu
乡村农夫4 小时前
cuda 环境搭建
linux
tingting01195 小时前
Linux 普通用户禁用sudo su - 命令
linux·运维·服务器
WZF-Sang5 小时前
Linux—进程学习-01
linux·服务器·数据库·学习·操作系统·vim·进程