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:列出根目录/下的内容,判断其是文件还是目录并输出里不同,大家可以对比一下这两种方法 。

相关推荐
Python私教2 分钟前
ubuntu搭建k8s环境详细教程
linux·ubuntu·kubernetes
羑悻的小杀马特15 分钟前
环境变量简介
linux
小陈phd1 小时前
Vscode LinuxC++环境配置
linux·c++·vscode
是阿建吖!1 小时前
【Linux】进程状态
linux·运维
明明跟你说过1 小时前
Linux中的【tcpdump】:深入介绍与实战使用
linux·运维·测试工具·tcpdump
Komorebi.py2 小时前
【Linux】-学习笔记05
linux·笔记·学习
Mr_Xuhhh2 小时前
重生之我在学环境变量
linux·运维·服务器·前端·chrome·算法
内核程序员kevin6 小时前
TCP Listen 队列详解与优化指南
linux·网络·tcp/ip
朝九晚五ฺ10 小时前
【Linux探索学习】第十四弹——进程优先级:深入理解操作系统中的进程优先级
linux·运维·学习
自由的dream10 小时前
Linux的桌面
linux