Bash 初学者系列#7:使用 If Else 和案例语句进行决策

让我们的 bash 脚本智能!

在这部分 bash 初学者系列,您将学习如何在您的 bash 脚本使其在不同的场景和案例中表现不同。

这样您就可以更高效地构建 bash 脚本,您还可以在脚本中实现错误检查。

在中使用 if 语句 bash

任何决策结构中最基本的构造都是 if 条件。 基本 if 语句的一般语法如下:

if [ condition ]; then
  your code
fi

if 语句以 fi (如果相反)。

注意留白!

  • 左方括号和右方括号与您所写的条件之间必须有一个空格。 否则,shell 会报错。
  • 条件运算符(=、==、<= 等)前后必须有空格。 否则,您将看到类似“预期一元运算符”的错误。

现在,让我们创建一个 example 脚本 根.sh。 仅当您以 root 用户身份运行脚本时,此脚本才会回显“您是 root”语句:

#!/bin/bash

if [ $(whoami) = 'root' ]; then
	echo "You are root"
fi

whoami 命令输出用户名。 来自 bash 变量教程,你知道的 $(command) 语法用于命令替换,它为您提供命令的输出。

条件 $(whoami) = 'root' 仅当您以 root 用户身份登录时才会为真。

不相信我? 你不必。 运行它并亲自查看。

在中使用 if-else 语句 bash

您可能已经注意到,当您运行 根目录 脚本作为普通用户。 当 if 条件被评估为 false 时,您想要运行的任何代码都可以包含在 else 语句中,如下所示:

#!/bin/bash

if [ $(whoami) = 'root' ]; then
	echo "You are root"
else
	echo "You are not root"
fi

现在,当您以普通用户身份运行脚本时,您会被提醒您不是全能的 root 用户:

[email protected]:~$ ./root.sh
You are not root

使用 else if 语句 bash

每当您想同时测试多个表达式(条件)时,都可以使用 elif (else-if) 语句。

为了 example, 以下 年龄.sh bash 脚本将您的年龄作为参数,并将输出与您的年龄相对应的有意义的消息:

#!/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

现在运行几次 年龄.sh 测试不同年龄的脚本:

[email protected]:~$ ./age.sh 11
You are a kid.
[email protected]:~$ ./age.sh 18
You are a teenager.
[email protected]:~$ ./age.sh 44
You are an adult.
[email protected]:~$ ./age.sh 70
You are an elder.

请注意,我使用了 -lt (小于)使用 $AGE 变量的测试条件。

另请注意,您可以拥有多个 elif 声明,但只有一个 else if 构造中的语句,并且必须以 fi.

在中使用嵌套的 if 语句 bash

您还可以在另一个 if 语句中使用 if 语句。 为了 example,看看下面的 天气.sh bash 脚本:

#!/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 "It's freezing outside ..."
fi

该脚本将任何温度作为参数,然后显示一条反映天气情况的消息。 如果温度大于五,则评估嵌套(内部)if-elif 语句。 让我们运行一些脚本来看看它是如何工作的:

[email protected]:~$ ./weather.sh 0
It's freezing outside ...
[email protected]:~$ ./weather.sh 8
The weather is cold.
[email protected]:~$ ./weather.sh 16
The weather is nice.
[email protected]:~$ ./weather.sh 30
The weather is hot.

在中使用 Case 语句 bash

您还可以在中使用 case 语句 bash 替换多个 if 语句,因为它们有时令人困惑且难以阅读。 case 构造的一般语法如下:

case "variable" in
	"pattern1" )
		Command … ;;
	"pattern2" )
		Command … ;;
	"pattern2" )
		Command … ;;
esac

注意!

  • 图案后面总是有一个空格和 ).
  • 命令后面总是跟双分号 ;;. 空格之前不是强制性的。
  • 案例陈述以 esac (大小写相反)。

在处理模式匹配或正则表达式时,Case 语句特别有用。 为了演示,请看以下内容 字符.sh bash 脚本:

#!/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

该脚本将一个字符作为参数,并显示该字符是小/大字母、数字还是特殊字符。

[email protected]:~$ ./char.sh a
Small Alphabet.
[email protected]:~$ ./char.sh Z
Big Alphabet.
[email protected]:~$ ./char.sh 7
Number.
[email protected]:~$ ./char.sh $
Special Character.

请注意,我使用了通配符星号

定义默认情况,它相当于 if 条件中的 else 语句。 bash

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

.

我在下表中列出了一些最流行的测试条件:健康)状况
相等的$a -lt $b
$a < $b$a -gt $b
$a > $b$a -le $b
$a <= $b$a -ge $b
$a >= $b$a -eq $b
$a 等于 $b$a -ne $b
$a 不等于 $b-e $文件
$FILE 存在-d $文件
$FILE 存在并且是一个目录。-f $文件
$FILE 存在并且是一个常规文件。-L $文件
$FILE 存在并且是一个软链接。$STRING1 = $STRING2
$STRING1 等于 $STRING2$STRING1 != $STRING2
$STRING1 不等于 $STRING2-z $STRING1

$STRING1 为空

[email protected]:~$ man test

幸运的是,您不需要记住任何测试条件,因为您可以在测试手册页中查找它们: 让我们创建一个名为 文件类型.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

检测文件是常规文件、目录还是软链接:

我通过添加对参数数量的检查来稍微改进了脚本。 如果没有参数或多个参数,脚本将输出一条消息并退出,而不运行脚本中的其余语句。

[email protected]:~$ ./filetype.sh weather.sh
weather.sh is a regular file.
[email protected]:~$ ./filetype.sh /bin
/bin is a soft link.
[email protected]:~$ ./filetype.sh /var
/var is a directory.
[email protected]:~$ ./filetype.sh 
Error: Invalid number of arguments

让我们运行几次脚本以使用各种类型的文件对其进行测试:

奖励:一行中的 Bash if else 语句 bash 到目前为止,您看到的所有 if else 语句都用于适当的

脚本。 这是一种体面的做法,但你没有义务这样做。 bash当您只想在 shell 本身中查看结果时,您可以在单行中使用 if else 语句

. bash 假设你有这个

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

您可以在一行中使用所有 if else 语句,如下所示:

您可以将上述内容复制并粘贴到终端中,然后自己查看结果。

基本上,您只需在命令后添加分号,然后添加下一个 if-else 语句。 bash 惊人的! 这应该让您对 Bash 中的条件语句有一个很好的理解。 我希望你喜欢制作你的

脚本更智能! bash 你可以通过练习一些简单的方法来测试你新学到的知识

下面 PDF 中的练习。 它也包括解决方案。

.a{fill:none;stroke:currentColor;stroke-linecap:round;stroke-linejoin:round;stroke-width:1.5px;}

下载圈 bash 请继续关注下周,因为您将学习在您的

脚本。