adb shell正则表达式无法在本地测试 [英] adb shell regular expression doesn't work as tested locally
问题描述
首先,对不起,如果我的问题不清楚或格式不便。这是我的第一篇文章:D。
First, sorry if my question is obscure or in an inconvenient format. This is my first post here :D.
我的问题是我有一个脚本,比方说 test.sh
读取输入,并验证它是否为正整数(此帖子中使用的正则表达式:
):
My issue is that I have a script, let's say test.sh
which reads an input, and validates if it's a positive integer (reg ex used from this post:
BASH: Test whether string is valid as an integer?):
#!/bin/sh
echo -n " enter number <"
read num
if [[ $num =~ ^-?[0-9]+$ ]] #if num contains any symbols/letters
then # anywhere in the string
echo "not a positive int"
exit
else
echo "positive int read"
fi
我正在使用adb shell在Android设备(Xiaomi Mi3 w)上运行此脚本,并且错误:
语法错误: =〜
意外的运算符不断显示。
I am running this script on my android device (Xiaomi Mi3 w) using adb shell and the error:
syntax error: =~
unexpected operator keeps displaying.
首先,我的正则表达式是否正确?
其次,关于如何克服此语法错误的任何提示?
First, is my regex even correct? Second, any hints on how I can overcome this syntax error?
推荐答案
我必须使用如图所示的ksh表达式下面的内容来使其正常工作。
I had to use ksh expression as shown below to get this to work.
case $num in
+([0-9])*(.)*([0-9]) )
# Variable positive integer
echo "positive integer"
;;
*)
# Not a positive integer
echo "NOPE"
exit
;;
esac
这篇关于adb shell正则表达式无法在本地测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!