Doubt involving multiple conditions in an IF using Whiptail(TUI) in Shell Script

Asked

Viewed 117 times

0

I am making a simple configuration file in Shell script with a TUI(Text User Interface) using Whiptail, I need to restrict the user input so that the program accepted as input only a range of integer numbers, I was able to make it work manually, but impossible to define large intervals. follows a passage...

That way it works perfectly, it’s just a "dumb" way to do and impractical to set large intervals Status2 is the output status is (0 to confirm and 1 to cancel) range is the value entered by the user


intervalo=$(whiptail --title "Entre com o valor em sugundos do intevalo de verificação do ruído ambiente" --inputbox "Entre com um inteiro de 0 a 9 (recomendado:1)" --fb 10 80 3>&1 1>&2 2>&3)
status2=$? #echo "O status2 foi $status2 e intervalo= $intervalo" #sleep 4
if (( $status2 == 0  &&  $intervalo == 1 || $status2 == 0  &&  $intervalo == 2 || $status2 == 0  &&  $intervalo == 3 || $status2 == 0  &&  $intervalo == 4 || $status2 == 0  &&  $intervalo == 5 || $status2 == 0  &&  $intervalo == 6 || $status2 == 0  &&  $intervalo == 7 || $status2 == 0  &&  $intervalo == 8 || $status2 == 0  &&  $intervalo == 9 )); then
        echo "Intervalo = $intervalo"
else
        echo "Configuração cancelada."
        exit 0
fi

What is the simplest correct way to define the multiple conditions of this loop if?? Something like: if (( $status2 == 0 && $intervalo > 0 && $intervalo < 10); then...

So it does not work and does not restrict decimal numbers.

1 answer

0

That would be your corrected code:

intervalo=$(whiptail --title "Entre com o valor em sugundos do intevalo de verificação do ruído ambiente" --inputbox "Entre com um inteiro de 0 a 9 (recomendado:1)" --fb 10 80 3>&1 1>&2 2>&3)
status2=$?
if [ $intervalo -ge 0 -a $intervalo -le 9 ]; then
    echo "Intervalo = $intervalo"
else
    echo "Configuração cancelada."
    exit 0
fi

And in fact the simplest way would be to make an if with a range that is the >= 0 and <= 9 that inside if [ ]; would be like:

-le 0 -a -ge 9

and second also that you were taking the test on $Status2 and not in the capture of data which is the variable $intermission

Browser other questions tagged

You are not signed in. Login or sign up in order to post.