本文介绍了如何测试字符串是否与POSIX Shell中的正则表达式匹配? (不是bash)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用的是Ubuntu系统外壳,而不是bash,我发现常规方法不起作用:
I'm using Ubuntu system shell, not bash, and I found the regular way can not work:
#!/bin/sh string='My string'; if [[ $string =~ .*My.* ]] then echo "It's there!" fi错误[[:找不到!
我该怎么做才能解决这个问题?
What can I do to solve this problem?
推荐答案[[ ... ]]是bash-ism.您可以通过将grep与常规if一起使用来使测试与外壳程序无关:
The [[ ... ]] are a bash-ism. You can make your test shell-agnostic by just using grep with a normal if:
if echo "$string" | grep -q "My"; then echo "It's there!" fi更多推荐
如何测试字符串是否与POSIX Shell中的正则表达式匹配? (不是bash)
发布评论