Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
227 views
in Technique[技术] by (71.8m points)

android - adb shell regular expression doesn't work as tested locally

First, sorry if my question is obscure or in an inconvenient format. This is my first post here :D.

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The default shell in Android is mksh. It is not 100% compatible with bash. So do not expect all bash recipes to work without changes.

For the description of features supported by mksh - read its manual page.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...