linux - 检查 Unix 中的变量是否包含文件 bash shell 中提到的任何字符串

标签 linux bash shell unix

<分区>

我有一个用例,需要在 OR 条件下使用多个子字符串检查变量并生成输出;如下:

if [ ! -z "$var" ]; then
   if [[ $var =~ .*Focus.* ]] || [[ $var =~ .*Wallet.* ]]; then
      ser=`echo $var|cut -d ' ' -f 2`
      echo " $ser is correct"
   else
      ser=`echo $var|cut -d ' ' -f 2`
      echo "!!!! $ser is incorrect !!!!"
    fi
fi

var 包含 pid 和 ServiceName。正在检查 ServiceName 是否包含这两个子字符串中的任何一个。它运行得很好。但我希望 var 在文件中提到的子字符串列表中进行检查,以便稍后可以修改该文件以获得更多子字符串。

最佳答案

参数匹配可以使用扩展模式:

#! /bin/bash

check=$(tr '\n' '|' < list_of_terms.txt)
check="@(${check%|})"

for service in Service1 GetFocus WalletLost ; do
    if [[ $service == *$check* ]] ; then
        echo $service matches
    fi
done

关于linux - 检查 Unix 中的变量是否包含文件 bash shell 中提到的任何字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52763429/

相关文章:

c - Linux 4.4 PCIe DMA 进入用户空间页面不工作 - highmem 不可用于 DMA?

linux - Bash 脚本的 SSH(SSH 代理)

bash - 如果 bash 脚本或它创建的任何后台作业发生错误,则退出

python - 可以在编程语言的命令行窗口中定义函数吗?

linux - 有没有办法在不重建其依赖项的情况下强制重建目标(make -B)?

linux - uuencode 无法正常使用我的以下命令附加文件

bash - 如何在 Shell 脚本中进行日期计算?

c - 无限递归 main() 中出现段错误后的不同运行时间

linux - openssl 签名校验失败 : "RSA_padding_check_PKCS1_type_1:block type is not 01"

linux - Mono AOT 编译程序的事后调试