bourne shell 脚本中的正则表达式匹配运算符是什么? [英] What is the regex matching operator in bourne shell script?
本文介绍了bourne shell 脚本中的正则表达式匹配运算符是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试根据正则表达式验证用户输入.
I am trying to validate user input against a regular expression.
vari=A
if [ $vari =~ [A-Z] ] ;
then
echo "hurray"
fi
我得到的输出是 swf.sh[3]: =~: unknown test operator.
The output I am getting is swf.sh[3]: =~: unknown test operator.
你能告诉我我可以使用的测试运算符吗?
Can you please let me know the test operator I can use?
推荐答案
它不是内置在 Bourne shell 中的,您需要使用 grep:
It's not built into Bourne shell, you need to use grep:
if echo "$vari" | grep -q '[A-Z]'; then
echo hurray
fi
如果你想匹配整个字符串,记得使用正则表达式锚,^
和 $
.请注意,-q
标志使 grep 安静,因此它唯一的输出是返回值,用于匹配/不匹配.
If you want to match the whole string, remember to use the regex anchors, ^
and $
. Note that the -q
flag makes grep quiet, so its only output is the return value, for match/not match.
这篇关于bourne shell 脚本中的正则表达式匹配运算符是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文