Anas Slim Anas Slim - 3 years ago 90
Bash Question

How continue processing shell script after respect of condition

in my shell script, I am asking a question and verifying the result with 2 conditions. What is I wanna do is to continue reading the rest of script if these conditions are true otherwise the question must always be asking user.

Other thing, it must be a function because I will use it many times.

And I purposely simplified the code:

#!/bin/bash

t="5"

function ask_version {
while true ; do

echo -ne "\033[32m$1\033[0m : "
read -n 1 -r

if [[ ! $REPLY =~ ^[0-9] ]] ; then
echo -e " Please respect number format"
elif [[ $t -ne $REPLY ]] ; then
echo -e "It is not the true number"
else
return 0
fi

done
}

ask_version "Please type a number"
if [[ $? = 0 ]]
#Here I wanna do the test, if true I continue...
fi

Answer Source

If im not wrong it seems that you need something like this:

While your conditions is not respected repeat the function and ask to type a number. When all conditions are met break and exit the script.

 while true; do
    ask_version "Please type a number"
    if (( $? == 0 )); then 
      #continue with the script
      #if all conditions are ok
      echo "Exiting"
      break
    else
      #something wrong
    fi
  done
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download