我正在使用Ubuntu系统外壳,而不是bash,但是我发现常规方法无法正常工作:
#!/bin/sh
string='My string';
if [[ $string =~ .*My.* ]]
then
echo "It's there!"
fi
错误[[:找不到!
如何解决此问题?
[[ ... ]]
是一种bash-ism。您可以通过仅将grep
与常规if
结合使用来使您的测试与外壳程序无关:
if echo "$string" | grep -q "My"; then
echo "It's there!"
fi
为什么将grep
您可以使用expr
:
[ "${string#*My}" != "$string" ] && echo "It's there!"
${...}