shell 循环 判断

for 循环

Shell 脚本里最简单的循环当属 for 循环。最简单的 for 循环如下所示,你只需将变量值依次写在 in 后面即可:

#!/bin/bash

for num in 1 2 3 4
do
    echo $num
done

如果要循环的内容是字母表里的连续字母或连续数字,那么就可以按以下语法来写脚本:

#!/bin/bash

for x in {a..z}
do
    echo $x
done
while 循环

除了 for 循环,Shell 同样提供了 while 循环。对于其它语言,如果你见过 for 循环却没见过 while 循环,那么你一定是学了个假语言。

在 while 循环里,每进行一次循环,条件都会被判断一次,来确定本次循环是否该继续。其实在循环次数比较少的情况下,for 循环与 while 循环效果差不多,但如果循环次数比较多,比如 10 万次,那么 while 循环的优势就体现出来了。

#!/bin/bash

n=1

while [ $n -le 4 ]
do
    echo $n
    ((n++))
done
循环套循环

循环是可以互相嵌套的。比如下面这个例子,我们在 while 循环里再套入一个 for 循环:

#!/bin/bash

n=1

while [ $n -lt 6 ]
do
    for l in {a..d}
    do
        echo $n$l
    done
    ((n++))
done

这个脚本执行的结果应该是 1a, 1b, 1c, 1d, 2a, 2b ... 5d。

循环的内容是变化的

我们上面提到的 for 循环,循环变量要赋的值都列在了 in 后面的列表里了。但这样灵活性太差,因为在很多情况下,循环变量要获得的值是不固定的。

就比如,有个变量要获得当前系统上所有用户,但因为每台电脑用户都不一样,我们根本就没办法将这个变量写死。

在这种情况下,我们可以使用 ls 命令将 /home 目录下所有用户都列出来,然后用循环变量依次获取它们。完整代码如下:

#!/bin/bash

for user in `ls /home`
do
    echo $user
done

当然,除了 ls ,Shell 还支持其它命令。比如我们可以使用 date 命令获取当前系统时间,再依次打印出来:

$ for word in `date`
> do
>     echo $word
> done
Thu
Apr
9
08:12:09
CST
2020
变量值检查

我们在使用 while 循环时,经常需要判断一个变量的值是否大于或者小于某个数。有时候这个数也是用另一个变量来表示,那么我们就需要判断这个变量的值是否是数字。有三种判断方法:

#!/bin/bash

echo -n "How many times should I say hello? "
read ans

if [ "$ans" -eq "$ans" ]; then
    echo ok1
fi

if [[ $ans = *[[:digit:]]* ]]; then
    echo ok2
fi

if [[ "$ans" =~ ^[0-9]+$ ]]; then
    echo ok3
fi

第一种方法看起来似乎是个废话,但实际上,-eq 只能用于数值间判断,如果是字符串则判断不通过,所以这就保证了 ans 是个数值型变量。

第二种方法是直接使用 Shell 的通配符对变量进行判断。

第三种方法就更直接了,使用正则表达式对变量进行判断。

我们直接来看一个例子:

#!/bin/bash

echo -n "How many times should I say hello? "
read ans

if [ "$ans" -eq "$ans" ]; then
  n=1
  while [ $n -le $ans ]
  do
    echo hello
    ((n++))
  done
fi

在这个脚本里,我将要循环的次数传入到 ans 变量,然后脚本就具体打印几次 hello 。为了保证我们传入的内容是数字,我们使用了 if [ "$ans" -eq "$ans" ] 语句来判断。如果我们传入的不是数字,则不会进入 while 循环。

循环输出文本文件内容

如果你想按行依次循环输出文本文件的内容,可以这样操作:

#!/bin/bash

echo -n "File> "
read file
n=0

while read line; do
  ((n++))
  echo "$n: $line"
done < $file

在这里,我们使用 read 命令将文本文件的内容读取存入 file 变量,然后再使用重定向(上述脚本最后一行)将 file 内容依次传入 while 循环处理再打印出来。

死循环

有时候我们需要一直永远循环做某件事,那么我们就可以使用死循环。达到这个目的很简单,只需使用 while true 即可。

#!/bin/bash

while true
do
    echo -n "Still running at "
    date
    sleep 1
done

在以上这个脚本里,将每隔 1 秒打印一次 Still running at 具体时间 ,直到你按 Ctrl + C 终止这个脚本。

判断
 

在 Shell 中有两种判断格式,分别如下:

# 1. 第一种
test  条件判断式

# 2. 第二种,注意括号两端必须有空格
[  条件判断式 ]    

第二种方式相当于第一种的简化。那么我们如何知道一个条件判断语句是否为真呢? 
我们学习过如何判断一个命令是否执行成功,即 $? 是否等于 00表示执行成功,否则表示上个命令失败,条件判断也是使用这种方式。

# 查看文件列表
[root@VM-0-5-centos ~]# ls
if.sh  student.txt  test.sh

# -e 文件名,用于判断文件是否存在
[root@VM-0-5-centos ~]# test -e if.sh
[root@VM-0-5-centos ~]# echo $?
0

[root@VM-0-5-centos ~]# test -e if.ssss
[root@VM-0-5-centos ~]# echo $?
1

# 换个姿势,再来测试一遍
[root@VM-0-5-centos ~]# [ -e if.sh ]
[root@VM-0-5-centos ~]# echo $?
0
[root@VM-0-5-centos ~]# [ -e if.ssss ]
[root@VM-0-5-centos ~]# echo $?
1

if 语句

  • if 开头,fi 结尾
  • [ 条件判断 ] 就是使用 test 命令判断,两端必须有空格
  • if 如果 和 then 在一行,需要加 ;

单分支

if [  条件判断式 ];then
 命令
fi

或者 

if [  条件判断式 ]
 then
  命令
fi

双分支

if [  条件判断式 ]
 then
  命令
else
  命令
fi

多分支

if [ 条件判断式1 ]
 then
  命令
elif [ 条件判断式2 ]
 then 
  命令
...

else
 命令
fi

条件判断类型

按照文件类型进行判断

shell 循环 判断_第1张图片

# 1. 新建一个脚本文件
[root@VM-0-5-centos ~]# vim file_test.sh

#!/bin/bash

read -p "please input filename: " filename

if [ -e $filename ]
  then
        echo "yes"
else
        echo "no"
fi

# 2. 添加可执行权限
[root@VM-0-5-centos ~]# chmod 755 file_test.sh

# 3. 测试执行
[root@VM-0-5-centos ~]# ./file_test.sh
please input filename: student.txt
yes
[root@VM-0-5-centos ~]# ./file_test.sh
please input filename: falsfja
no

为了测试各种判断类型方便,我们可以直接使用如下方式测试,避免每次写脚本了。

# 一个命令正确执行,输出yes,否则输出no
[root@VM-0-5-centos ~]# [ -e student.txt ] && echo 'yes' || echo 'no'
yes
[root@VM-0-5-centos ~]# [ -e sss ] && echo 'yes' || echo 'no'
no


[root@VM-0-5-centos ~]# [ -d mydir/ ] && echo 'yes' || echo 'no'
yes
[root@VM-0-5-centos ~]# [ -d student.txt ] && echo 'yes' || echo 'no'
no

按照文件权限进行判断

[root@VM-0-5-centos ~]# [ -x file_test.sh ] && echo 'yes' || echo 'no'
yes

[root@VM-0-5-centos ~]# [ -x student.txt ] && echo 'yes' || echo 'no'
no

文件之间比较

# 创建硬链接后测试
[root@VM-0-5-centos ~]# ln student.txt /tmp/student.txt

[root@VM-0-5-centos ~]# [ student.txt -ef /tmp/student.txt ] && echo 'yes' || echo 'no'
yes

[root@VM-0-5-centos ~]# [ student.txt -ef /tmp/stargate.lock ] && echo 'yes' || echo 'no'
no

整数之间比较

shell 循环 判断_第2张图片

[root@VM-0-5-centos ~]# [ 10 -eq 10 ]  && echo 'yes' || echo 'no'
yes

[root@VM-0-5-centos ~]# [ 10 -gt 5 ]  && echo 'yes' || echo 'no'
yes

[root@VM-0-5-centos ~]# [ 10 -lt 5 ]  && echo 'yes' || echo 'no'
no

字符串的判断

shell 循环 判断_第3张图片

if 判断中对于变量的处理,需要加引号,如果没有加双引号,可能会在判断含空格的字符串变量的时候产生错误。

[root@VM-0-5-centos ~]# name=""

# 不见引号,判断出的 name 是非空,其实是空
[root@VM-0-5-centos ~]# [ -n $name ] && echo 'yes' || echo 'no'
yes

# 加上引号就对了
[root@VM-0-5-centos ~]# [ -n "$name" ] && echo 'yes' || echo 'no'
no


[root@VM-0-5-centos ~]# name1=hello
[root@VM-0-5-centos ~]# name2=world
[root@VM-0-5-centos ~]# [ "$name1" != "$name2" ] && echo 'yes' || echo 'no'
yes
[root@VM-0-5-centos ~]# [ "$name1" == "$name2" ] && echo 'yes' || echo 'no'
no

多重条件判断

[root@VM-0-5-centos ~]# a=hello

[root@VM-0-5-centos ~]# [ -n "$a" -a "$a" == "hello" ] && echo 'yes' || echo 'no'
yes

[root@VM-0-5-centos ~]# [ -n "$a" -a "$a" == "world" ] && echo 'yes' || echo 'no'
no

你可能感兴趣的:(linux,运维)