shell编程中for/while/until循环命令(2)

1.6用通配符读取目录
[root@sh shell]# cat for6.sh 
#!/bin/bash
for file in /home/*
do
 
  if [ -d "$file" ];then
    echo "$file is a directory"
  elif [ -f "$file" ];then
    echo "$file is a file"
  fi
 
done
 
[root@sh shell]# sh for6.sh 
/home/apache-tomcat-8.0.28.tar.gz is a file
/home/dir1 is a directory
/home/dir2 is a directory
/home/fie1 is a file
/home/fie2 is a file
/home/fie22 is a file

[root@sh shell]# cat for7.sh 
#!/bin/bash
for file in /home/* /home/badtest
do
 
  if [ -d "$file" ];then
    echo "$file is a directory"
  elif [ -f "$file" ];then
    echo "$file is a file"
  else
    echo "$file doesn't exist"
  fi
 
done
[root@sh shell]# sh for7.sh 
/home/apache-tomcat-8.0.28.tar.gz is a file
/home/dir1 is a directory
/home/dir2 is a directory
/home/fie1 is a file
/home/fie2 is a file
/home/fie22 is a file
/home/badtest doesn't exist

二、C语言风格的for命令
2.1 C语言风格的for命令
    C语言的for命令有一个用来指明变量的特殊方法、一个必须保持成立才能继续迭代的条件,以及另一个为每个迭代改变变量的方法。当指定的条件不成立时,for循环就会停止。条件等式通过标准的数字符号定义。
    for (i=0; i<10; i++)

{
        printf("The next number is %d\n",i):

}

第一部分将一个默认值赋给该变量,中间的部分定义了循环重复的条件,当定义的条件不成立时,for循环就停止迭代,最后一部分定义了迭代的过程。

bash中C语言风格的for循环的基本格式:
    for (( variable assignment;condition;iteration process))

for (( a = 1; a < 10; a++ ))

[root@sh shell]# cat c1.sh 
#!/bin/bash
 
for (( i=1; i < 10; i++ ))
do
  echo "The next number is $i"
done
 
[root@sh shell]# sh c1.sh 
The next number is 1
The next number is 2
The next number is 3
The next number is 4
The next number is 5
The next number is 6
The next number is 7
The next number is 8
The next number is 9

2.2使用多个变量

C语言风格的for命令也允许你为迭代使用多个变量。循环会单独处理每个变量,允许你为每个变量定义不同的迭代过程。当你有多个变量时,你只能在for循环中定义一种条件:
[root@sh shell]# cat c2.sh 
#!/bin/bash
 
for (( a=1, b=10; a <= 10; a++, b-- ))
do
  echo "$a - $b"
done
 
[root@sh shell]# sh c2.sh 
1 - 10
2 - 9
3 - 8
4 - 7
5 - 6
6 - 5
7 - 4
8 - 3
9 - 2
10 - 1

三、while命令
    while命令在某种意义上是if-then语句和for循环的混杂体。while命令允许你定义一个要测试的命令,然后循环执行一组命令,只要定义的测试命令返回的是退出状态码0,它就会在每个迭代的一开始测试test命令,在测试test命令返回非零退出状态码是,while命令会停止执行那组命令。

3.1while的基本格式
    while test command

do

other command

done

while命令指定的test命令的退出状态码必须随着循环中运行的命令改变,如果退出状态码从不改变,那么while循环将会一直不停地循环。

[root@sh shell]# cat w1.sh 
#!/bin/bash
 
var1=10
while [ $var1 -gt 0 ]
do
  echo $var1
  var1=$[ $var1 - 1 ]
done
 
[root@sh shell]# sh w1.sh 
10
9
8
7
6
5
4
3
2
1

在这些命令中,测试条件中用到的变量必须被修改,否则你就进入了一个无限循环。

3.2使用多个测试命令
    while命令允许你在while语句行定义多个测试命令,只有最后一个测试命令的退出状态码会被用来决定什么时候介绍循环。

12345678910111213141516171819202122232425262728293031323334353637 [root@sh shell]# cat w2.sh 
 
#!/bin/bash
 
var1=10
 
while echo $var1
    [ $var1 -ge 0 ]
do
  echo "This is inside the loop"
  var1=$[ $var1 - 1 ]
done
 
[root@sh shell]# sh w2.sh 
10
This is inside the loop
9
This is inside the loop
8
This is inside the loop
7
This is inside the loop
6
This is inside the loop
5
This is inside the loop
4
This is inside the loop
3
This is inside the loop
2
This is inside the loop
1
This is inside the loop
0
This is inside the loop
-1

在上面的例子中,while语句中定义了两个测试命令:

while echo $var1

[ $var1 -ge 0 ]

在while语句中,在每次迭代中所有的测试命令都会被执行,包括最后一个命令不成立的最后那次循环。注意每个测试命令都是在单独的一行上。

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/795d91d3d38dd52bc484fcd3c522869c.html