在 Bash 中的 Case 语句中运行正则表达式
本文探讨了正则表达式、它们的基本语法,以及如何在 Bash 中使用 case 结构和 if-else 结构来运行它们。
正则表达式简介
正则表达式,也称为 regex 或 regexp,是用于文本/字符串匹配的字符序列。 正则表达式非常强大,当您需要解析大量数据时会节省大量时间。
虽然 Bash 不使用正则表达式,但它使用字符串匹配,其语法类似于正则表达式。 下面的脚本将帮助您熟悉使用 Bash 进行字符串匹配的基础知识。
?(a pattern list)
# Matches exactly zero or one instance of the pattern
*(a pattern list)
# This matches zero or more instances of the pattern.
+(a pattern list)
# This matches one or more instances of the pattern.
@(a pattern list)
# This matches one of the enclosed patterns.
!(a pattern list)
# This matches any pattern except the one enclosed.
上面的代码栏展示了基本的正则表达式语法; 如果您想了解更多关于正则表达式的信息。
在 Bash 中启用复杂字符串匹配的需要
默认情况下,您可以在 Bash 中运行简单的正则表达式; 复杂的正则表达式将要求您启用 extglob 选项。 以下命令将向您展示如何启用或禁用 extglob 以允许您运行复杂的正则表达式。
shopt -s extglob # this command enables extglob and allows the execution of complicated regex
shopt -u extglob # this command disables extglob and disables execution of complicated regex
上面命令中的 extglob 代表扩展的 globing。 如果设置,则允许在路径名扩展下给出的复杂/高级模式匹配功能。
必须注意的是,不运行一个正则表达式是不可能知道它是否复杂的。 启用 extglob 选项可以避免任何痛苦并无忧地运行所有命令。
您可能有一个问题是我们如何知道 extglob 是打开还是关闭。 请参阅以下命令以供参考。
shopt | grep extglob # displays status of extglob
输出:
extglob off # displays this line if extglob is disabled
extglob on # displays this line if extglob is enabled
上面的命令将显示 extglob 的状态,无论是打开还是关闭。
在条件结构中使用正则表达式进行字符串匹配
字符串匹配(类似于正则表达式)可以通过 case 结构变得更加强大,我们希望使用它来让我们自己解析更复杂的数据。 下面的命令将帮助您完成 Bash 中 case 的使用。
case EXPRESSION in
Match_1)
STATEMENTS # run this statement if in matches match_1
;;
Match_2)
STATEMENTS # run this statement if in matches match_2
;;
Match_N)
STATEMENTS # run this statement if in matches match_N
;;
*)
STATEMENTS # otherwise, run this statement
;;
Esac # signals the end of the case statement to the kernel
上面的代码是您将使用的通用语法,以防您想将 Bash 字符串匹配与 case 结构相结合。
默认情况下,Bash 允许简单的模式匹配。 例如,它将成功执行以下 Bash 命令。
cat sh*
# the above command displays the contents of all files whose name begins #with sh to the monitor (or stdout)
然而,给定下面的命令(使用复杂的模式匹配),你会得到一个错误 bash: syntax error near unexpected token '('.
cat +([0-9]) # this command displays contents of files whose names are
# entirely composed of numbers
如果您想进一步研究 Bash 中的字符串匹配,请使用以下命令。
man bash # man is a short form of manual pages here
手册页是一个实用程序,可用于查找有关任何 Bash 命令、系统调用等的信息。
如果您在 Bash 中使用字符串匹配和 case,最好先声明并定义一个您将比较模式的变量。
我们在下面的命令片段中使用了字符串匹配的案例。 请注意我们如何使用字符串匹配的基础知识来生成强大的字符串匹配算法。
让我们看看命令。
# Remember to not forget to enable extglob
shopt -s extglob # enables extglob
shopt | grep extglob # checks if extglob is enabled
some_variable="rs-123.host.com"; # declare and define variable
case $some_variable in
ab-+([0-9])\.host\.com) echo "First 2 characters were ab"
;;
ks-+([0-9])\.host\.com) echo "First 2 characters were ks"
;;
cs-+([0-9])\.host\.com) echo "First 2 characters were cs"
;;
*)echo "unknown first 2 characters"
;;
esac;
# the above command will display the unknown first 2 characters as we
# don't have a match in the first three cases, so the last default one will #automatically be true
输出:
unknown first 2 characters
如果分析上面的字符串匹配命令,如果前两个字符是(ab,ks,cs)之一,下一个字符是连字符(-)后跟任意数量的数字,以.host.com结尾,则其中之一 前三种情况都会成功,并会显示相应的消息。
然而,如果不是这种情况,那么默认值(即其他情况)将运行,我们将收到一条消息:未知的前 2 个字符。
如果您觉得上述命令太复杂,我们有一个更简单的解决方案。 下面的命令准确地解释了如何。
some_variable="rs-123.host.com"; # declare and define variable
case $some_variable in
ab*.host.com) echo "First 2 characters were ab"
;;
# the command below stays the same
上面的命令与我们上面使用的更复杂的 case 结构相同。
在 If-Else 结构中使用正则表达式进行字符串匹配
编写强大的字符串匹配算法的另一种方法是将它们与 if-else 结构一起使用。 我们想用它来解析更复杂的数据。
以下 Bash 命令将带您了解 if-else 结构的语法。
if expression1
then
task1
elif expression2
then
task2
else
task3
fi # signals ending of if else structure
使用 if-else 结构很容易; 首先,我们评估第一个表达式。
如果为真,我们执行任务 1。否则,我们考虑第二个表达式。
如果为真,则执行任务 2,否则执行任务 3。
现在您已经熟悉 if-else 结构的语法,让我们将 case 结构中使用的命令复制到等效的 if-else 结构。 请参阅下面的命令来执行此操作。
# Remember to not forget to enable extglob
shopt -s extglob # enables extglob
shopt | grep extglob # checks if extglob is enabled
some_variable="rs-123.host.com"; # declare and define variable
if expr "$some_variable" : ‘ab-+([0-9])\.host\.com’ >/dev/null; then echo "First 2 characters were ab"
elif expr "$some_variable" : ‘ks-+([0-9])\.host\.com’ >/dev/null; then echo "First 2 characters were ks"
elif expr "$some_variable" : ‘cs-+([0-9])\.host\.com’ >/dev/null; then echo "First 2 characters were cs"
else echo "unknown first 2 characters"
fi
# the above command will display the unknown first 2 characters as we
# don't have a match in the first three cases, so the last default one will #automatically be true
输出:
unknown first 2 characters
上面的命令是我们之前使用的 case 结构的 if-else 等价物。
相关文章
在 Bash 中运行 find -exec 命令
发布时间:2024/03/14 浏览次数:127 分类:操作系统
-
本文将演示如何使用 find 命令的 -exec 参数来使用 find 命令定位文件中的任何文本。