-1

I am getting error "If" and then"" unexpected in GNU sed command.How to check input continuously.Please help me

if ! [[ "$versionCode" =~ ^[0-9]+$ ]]
then
    echo "Sorry integers only"
fi

This is my error

sh: 1: Syntax error: end of file unexpected (expecting "then")

sh: 1: Syntax error: "then" unexpected

Sorry integers only
sh: 1: Syntax error: "fi" unexpected

update:

echo "Enter version code" 
read versionCode
case "$versionCode" in
   (*[!0-9]*) echo "Sorry integers only";;
   ("")       echo "Empty is not a version code";;
   (*)        echo "do something with $versionCode";;
 esac
echo "$versionCode"
sudo  sed "s/\(versionCode[[:space:]]*\)[0-9]*/\1${versionCode}/" Version.gradle

this is my error

Enter version code
4

sh: 1: Syntax error: end of file unexpected (expecting ")")

sh: 1: Syntax error: word unexpected

sh: 1: Syntax error: word unexpected

sh: 1: Syntax error: word unexpected

sh: 1: Syntax error: "esac" unexpected
Manoj
  • 27
  • 4

1 Answers1

0

The POSIXly portable way to do this is

 case "$versionCode" in
   (*[!0-9]*) echo "Sorry integers only";;
   ("")       echo "Empty is not a version code";;
   (*)        echo "do something with $versionCode";;
 esac
Jens
  • 69,818
  • 15
  • 125
  • 179
  • @Manoj Yes it does work. If it does not for you, you are doing something wrong. Please be specific in what you have done with my script and what error message you get. "Does not work" is not a helpful description. What system are you on? Which editor are you using? – Jens Apr 13 '20 at 10:03
  • @jeans I am using GNU sed script and I update my script with error that i got and I am using ubuntu as windows sub system – Manoj Apr 13 '20 at 10:18
  • @Manoj One problem could be the line endings on Windows (\r\n) versus Unix (just \n). Try the command Cyrus suggested above. Any output from that is a sign of line ending mismatch. – Jens Apr 13 '20 at 12:19