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] 

Linux shell编程学习笔记17:for循环语句_第1张图片

(二)格式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] 

Linux shell编程学习笔记17:for循环语句_第2张图片

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 ~]$  

 Linux shell编程学习笔记17:for循环语句_第3张图片

 (三)格式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] 

Linux shell编程学习笔记17:for循环语句_第4张图片

(四)格式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

Linux shell编程学习笔记17:for循环语句_第5张图片

导致出错的原因是命令

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] 

Linux shell编程学习笔记17:for循环语句_第6张图片

 (五)格式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] 

Linux shell编程学习笔记17:for循环语句_第7张图片

二、字符条件类循环

(一)格式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] 

Linux shell编程学习笔记17:for循环语句_第8张图片

(二)格式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 ~]$ 

Linux shell编程学习笔记17:for循环语句_第9张图片

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] 

Linux shell编程学习笔记17:for循环语句_第10张图片

 

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

 (一)实例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] 

 Linux shell编程学习笔记17:for循环语句_第11张图片

我们使用 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] 

Linux shell编程学习笔记17:for循环语句_第12张图片

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

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

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

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

你可能感兴趣的:(Linux世界,麒麟操作系统,系统维护,linux,学习笔记,linux,shell,脚本编程,for语句,循环控制语句,for循环)