当前位置 : 主页 > 手机开发 > harmonyos >

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

来源:互联网 收集:自由互联 发布时间:2023-12-16
Linux Shell 脚本编程和其他编程语言一样,支持算数、关系、布尔、字符串、文件测试等多种运算,同样也需要进行根据条件进行流程控制,提供了if、for、while、until等语句。 之前我们探


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

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

一、数字条件类循环

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

# cs @ 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# cs @ edu in ~ [22:15:25] 
 $

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

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

1.在zsh中 

# cs @ 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# cs @ edu in ~ [23:22:44] 
 $

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

2.在bash中

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

 

Linux shell编程学习笔记17:for循环语句_脚本编程_03

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

# cs @ 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# cs @ edu in ~ [23:28:42] 
 $

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

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

1.多余空格导致错误

# cs @ 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循环语句_linux_05

导致出错的原因是命令

for i in {1..10};  do

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

2.正确的格式

# cs @ 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# cs @ edu in ~ [23:36:47] 
 $

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

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

# cs @ 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# cs @ edu in ~ [22:41:57] 
 $

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

二、字符条件类循环

(一)格式1:in 字符串

1.在bash中有效

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

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

 2.在zsh中无效 

[cs ~]$ exec zsh

# cs @ 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# cs @ edu in ~ [14:00:55] 
 $

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

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

1.在bash中部分有效

# cs @ edu in ~ [14:26:43] 
 $ exec bash
 [cs ~]$ 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
 [cs ~]$ 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
 [cs ~]$ 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
 [cs ~]$ 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
 [cs ~]$ 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
 [cs ~]$

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

2.在zsh中有效

# cs @ 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# cs @ 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# cs @ edu in ~ [14:26:43] 
 $

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

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

1.在bash中有效

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

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

或者

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

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

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

Linux shell编程学习笔记17:for循环语句_脚本编程_14

2.在zsh中无效

[cs ~]$ exec zsh
# cs @ 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# cs @ edu in ~ [21:59:03] 
 $

 

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

  

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

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

1.在bash中

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

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

2.在zsh中

# cs @ 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# cs @ edu in ~ [23:12:14] 
 $

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

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

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

1.在bash中

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

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

2.在zsh中

[cs ~]$ exec zsh

# cs @ 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

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

(六)一些无效的格式

1.in 字母1-字母n

(1)在basth中
# cs @ edu in ~ [22:36:03] 
$ exec bash
 [cs ~]$ 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
 [cs ~]$

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

(2)在zsh中
# cs @ 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# cs @ edu in ~ [22:02:09] 
 $

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

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

(1)在bash中
[cs ~]$ 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

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

(2)在zsh中
# cs @ 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]# cs @ edu in ~ [22:28:11] 
 $

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

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

(1)在bash中

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

 

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

 (2)在zsh中
# cs @ 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]# cs @ edu in ~ [22:33:29] 
 $

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


 

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

(1)在bash中 
[cs ~]$ 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
 [cs ~]$

Linux shell编程学习笔记17:for循环语句_脚本编程_26

 (2)在zsh中
# cs @ 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# cs @ edu in ~ [22:36:03] 
 $

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

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

(1)在bash中
[cs ~]$ 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
 [cs ~]$

 

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

(2)在zsh中
# cs @ 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# cs @ edu in ~ [22:46:35] 
 $

Linux shell编程学习笔记17:for循环语句_脚本编程_29

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

(1)在bash中
# cs @ edu in ~ [22:51:56] 
 $ exec bash
 [cs ~]$ 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
 [cs ~]$

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

(2)在zsh中
# cs @ 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]# cs @ edu in ~ [22:51:56] 
 $

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

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

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

# cs @ edu in ~ [11:39:57] C:1
 $ cd /# cs @ 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# cs @ 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!# cs @ edu in / [11:43:15] 
 $

 

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

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

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

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

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

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

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

# cs @ edu in ~ [14:03:58] 
 $ ls *.sh  
 zsh: no matches found: *.sh# cs @ edu in ~ [14:05:37] C:1
 $ echo "hello" > hello.sh# cs @ edu in ~ [14:05:55] 
 $ cat hello.sh
 hello# cs @ 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 !# cs @ edu in ~ [14:07:16] 
 $

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

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

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

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

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

上一篇:C++二分查找算法的应用:最长递增子序列
下一篇:没有了
网友评论