我正在尝试在bash中比较字符串。我已经在stackoverflow上找到了如何执行此操作的答案。在我的脚本中,我正在使用Adam在提到的问题中提交的代码:
#!/bin/bash
string='My string';
if [[ "$string" == *My* ]]
then
echo "It's there!";
fi
needle='y s'
if [[ "$string" == *"$needle"* ]]; then
echo "haystack '$string' contains needle '$needle'"
fi
我还尝试了来自Ubuntu论坛的方法,你可以在第2篇帖子中找到。
if [[ $var =~ regexp ]]; then
#do something
fi
在这两种情况下,我都收到了错误消息:
[[: not found
我做错了什么?
/bin/bash --version
命令会打印什么? - themel[
不起作用,因为它不支持模式匹配。 - Ansgar Wiecherssh yourscript
而不是bash yourscript
;如果你使用sh
覆盖了#!/bin/bash
的 shebang,它将不起作用。 - undefined