bash - 如果字符串变量包含通配符,如何检查 shell 脚本?

标签 bash shell wildcard

我正在尝试检查字符串是否包含任何通配符。 这是我失败的尝试:

#!/bin/bash
WILDCARDS='* . ? !  ] [' 
a="foo*bar"
for x in $REJECTED_WILDCARDS
do 
    if [[ "$a" == *"$x"* ]]
    then 
            echo "It's there!";
    fi 
done

有什么建议吗?

最佳答案

稍短且没有循环:

if [ "$a" != "${a//[\[\]|.? +*]/}"  ] ; then
  echo "wildcard found"
fi

参数替换删除所有通配符。 字符串不再相等。

关于bash - 如果字符串变量包含通配符,如何检查 shell 脚本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16173569/

相关文章:

sql - Select * from Table 并仍然在单个命名列上执行某些功能

linux - Bash 的新手。简单脚本

linux - bash,如何将记录器脚本转换为函数

bash - 为什么 eval 使用 set -e 退出 subshel​​l mid-&&?

shell - 检查程序是否正在使用 bash shell 脚本运行?

java - 运行时已知的通用上限通配符实例化

bash - 读取文件的shell脚本

bash - 尝试做类似 rot13 的解决方案来处理特殊字符

shell - 如何在fish shell中导出函数

Java 泛型 : Understanding Bounded Wildcards