首页 > 行业资讯 > 正文

今天我们介绍一下如何在 bash 中使用条件语句。

在 bash 中使用 if 语句

在绝大多数编程语言中,if 语句都是最基本的条件语句。在 bash 中其语法如下:

复制if [ condition ]; then your code fi

if 语句以 fi(与if相反)结束。

注意空格:

在开始括号之后,与结束括号之前,都必须要有一个空格,否则 shell 将报错; 条件运算符(=,==,<=等)前后必须有空格,否则将报错。

我们创建一个示例脚本 root.sh,当你以 root 身份运行该脚本的时候,才会进入 if 语句:

复制#!/bin/bash if [ $(whoami) = root ]; then echo “You are root” fi

whoami 命令输出当前用户名。在 bash 变量教程中,我们介绍了 $(command) 结构的语法用于命令替换。

所以,当你以 root 用户身份登录时,条件 $(whoami) = root 才为真。

如何在bash中使用条件语句-巴氏刷牙法

if-else 语句

上述代码中,如果当前用户不是 root,将看不到任何输出。当 if 条件为 false 的时候,需要将其执行的代码放到 else 语句中,如下所示:

复制#!/bin/bash if [ $(whoami) = root ]; then echo “You are root” else echo “You are not root” fi

此时,当你以普通用户运行脚本的时候,会输出 You are not root,如下所示:

复制$ ./root.sh You are not root

使用 else if 语句

当有多个表达式(条件)时,可以使用 elif(else-if)语句。看下面的例子,我们创建一个名为 age.sh 的脚本:

复制#!/bin/bash AGE=$1 if [ $AGE -lt 13 ]; then echo “You are a kid.” elif [ $AGE -lt 20 ]; then echo “You are a teenager.” elif [ $AGE -lt 65 ]; then echo “You are an adult.” else echo “You are an elder.” fi

上述代码中的 -lt 即小于(less than)。在前面的文章中我们介绍过如何给 bash 脚本传递参数,现在我们运行一下 age.sh,并传递不同的参数:

复制$ ./age.sh 11 You are a kid. $ ./age.sh 18 You are a teenager. $ ./age.sh 44 You are an adult. $ ./age.sh 70 You are an elder.

需要注意的是,在条件语句中,可以有多个 elif ,但是只能有一个 else,且必须使用 fi 关闭。

在 bash 中嵌套使用 if 语句

if 语句可以嵌套使用。看如下 weather.sh 脚本:

复制#!/bin/bash TEMP=$1 if [ $TEMP -gt 5 ]; then if [ $TEMP -lt 15 ]; then echo “The weather is cold.” elif [ $TEMP -lt 25 ]; then echo “The weather is nice.” else echo “The weather is hot.” fi else echo “Its freezing outside …” fi

上述脚本接受温度作为参数,然后显示一条反应天气情况的信息。当温度大于 5 度的时候,就会进入嵌套的 if 语句。我们运行一下该脚本:

复制$ ./weather.sh 0 Its freezing outside … $ ./weather.sh 8 The weather is cold. $ ./weather.sh 16 The weather is nice. $ ./weather.sh 30 The weather is hot.

在 bash 中使用 case 语句

我们还可以在 bash 中使用 case 语句来替换多个 if 语句,其构造的一般语法如下:

复制case “variable” in “pattern1” ) Command … ;; “pattern2” ) Command … ;; “pattern2” ) Command … ;; esac

注意:

条件语句最后总会包含一个空格和右括号 ); 条件语句后的命令以两个分号 ;; 结束,其前面的空格可有可没有; case 语句 以 esac 结尾(与 case 相反)。

case 语句在处理模式匹配或正则表达式的时候特别有用。作为演示,我们创建一个名为 char.sh 的脚本:

复制#!/bin/bash CHAR=$1 case $CHAR in [a-z]) echo “Small Alphabet.” ;; [A-Z]) echo “Big Alphabet.” ;; [0-9]) echo “Number.” ;; *) echo “Special Character.” esac

上述脚本接受一个字符作为参数,并显示该字符是大写字母,还是小写字母、数字还是特殊字符。

复制$ ./char.sh a Small Alphabet. $ ./char.sh Z Big Alphabet. $ ./char.sh 7 Number. $ ./char.sh $ Special Character.

上述脚本中我使用了通配符 * 来定义默认字符,它相当于 if 语句中的 else 语句。

bash 中的测试条件

有许多测试条件可以与 if 语句一起使用。如果使用数字、字符串或文件,测试条件会有所不同。将它们视为bash中的逻辑运算符。

下表列出一些最常用的测试条件:

条件 相当于 a -ltb **a <**b a -gtb **a >**b a -leb **a <=**b a -geb **a >=**b a -eqb a 等于b a -neb a 不等于b -e**FILE **FILE 存在 -d**FILE **FILE 存在且是一个目录. -f**FILE **FILE 存在且是一个常规文件. -L**FILE **FILE 存在且是一个软链接. **STRING1 =**STRING2 STRING1 等于STRING2 **STRING1 !=**STRING2 STRING1 不等于STRING2 -z**STRING1 **STRING1 是空的

上面这些测试条件不需要专门记,可以在 man 手册中查看:

复制$ man test

我们创建一个名为 filetype.sh 的脚本,用来检查文件是常规文件、目录还是软链接:

复制#!/bin/bash if [ $# -ne 1 ]; then echo “Error: Invalid number of arguments” exit 1 fi file=$1 if [ -f $file ]; then echo $file is a regular file.” elif [ -L $file ]; then echo $file is a soft link.” elif [ -d $file ]; then echo $file is a directory.” else echo $file does not exist” fi

在脚本的开始,我们检查一下参数的数量,如果没有参数或者有多个参数,脚本会输出一条消息并退出。下面是运行情况:

复制$ ./filetype.sh weather.sh weather.sh is a regular file. $ ./filetype.sh /bin /bin is a soft link. $ ./filetype.sh /var /var is a directory. $ ./filetype.sh Error: Invalid number of arguments

在同一行中编写 if else 语句

到目前为止,我们上述代码都是在规范的缩进格式下写的,这是一种规范的写法,但并不是非这样写不可。

当你只想在 shell 中看到结果时,可以在单行中使用 if else 语句。

假设我们有如下脚本:

复制if [ $(whoami) = root ]; then echo “You are root” else echo “You are not root” fi

可以在一行代码中这样写:

复制if [ $(whoami) = root ]; then echo “root”; else echo “not root”; fi

大家可以运行一下上面的脚本,看看结果。

基本上,就是在命令结束后添加分号 ; ,然后写下一条语句。

猜你喜欢