我正在尝试检查字符串是否包含任何通配符。 这是我失败的尝试:
#!/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/