t4kmode t4kmode - 1 year ago 85
Android Question

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

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?):

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"
echo "positive int read"

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?

Answer Source

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"
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download