LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Bash user input valid then continue (https://www.linuxquestions.org/questions/linux-newbie-8/bash-user-input-valid-then-continue-4175533485/)

powerplyer 02-09-2015 04:33 AM

Bash user input valid then continue
 
I have a script which reads a controller ID and continues. I am trying to add some checks for user input "ctrl".

Valid responses would be whole number 1 thur 5. If value is between 1-5 (single numeric value) then continue with the rest of the script. If any other value then Exit script or continue to ask user input until valid.

I tried a couple of while loops do with esac, but can not seem to get a handle

Code:

read -p "Controller ID#: " ctrl

Any help would be greatly appreciated

pan64 02-09-2015 04:49 AM

So there are only 5 possible answers. You can do a simple case to check it or you can do arithmetic check too. But first here is an example: http://www.thegeekstuff.com/2010/07/...ase-statement/

veerain 02-09-2015 04:50 AM

Here it is:

Code:

read -p "Controller ID#: " ctrl
if [ $ctrl -le 5 ]; then
true
else
exit
fi


chrism01 02-09-2015 05:06 AM

This is one way
Code:

#!/bin/bash
#set -xv

while true
do
    read -p "Controller ID#: " ctrl
    match=0
    idx=5
    for i in $(seq $idx)
    do
        if [[ $i -eq $ctrl ]]
        then
            echo match
            match=1
            break
        fi
    done
done

# continue here if 1 - 5

The set cmd will help you debug in future

powerplyer 02-09-2015 09:49 AM

WOW thanks all for you suggestions. I will try it out this afternoon and let mark the ticket solved.

powerplyer 02-09-2015 10:24 AM

@veerain your suugestion works good, the only problem is when the user enters an invalid entry it errors out. Also since this you used the
Code:

if [ $ctrl -le 5 ]; then
is accepts the 0 value. I would like more of a graceful exit.
Code:

./test.sh: line 89: [: a: integer expression expected
@chrism01: your solution is working, however when the user selects the correct value. it just says "match" and exits. In my script if the ctrl value is valid I would like it to continue the script.

@pan64 thanks for your suggestion however I tried this in the past but do not know how to set up the regex.

Code:

case expression in $ctrl
    *^[1-5]* )
        "Continueing with script ;;
    *[a-Z,0,6-9]* )
        "Please Enter a whole number from 1-5 ONLY" ;;

esac

Basically ctrl is a varible I set-up on a more elobrate script.

pan64 02-09-2015 10:30 AM

you can do it without regexp:
Code:

case exp in $var
 1) do something;;
 2) do something else;;
 3) and so on;;
 4) ...
 ...
 *) something went wrong ...;;
ecac

(or something similar)

powerplyer 02-09-2015 12:13 PM

@pan64

thanks your help I got the case function to correctly.

Code:

# Validate User Input
    case $ctrl in
        1) echo -e ${grnw} ${open}"You have choosen controller 1"${end};;
        2) echo -e ${grnw} ${open}"You have choosen controller 2"${end} else;;
        3) echo -e ${grnw} ${open}"You have choosen controller 3"${end} else;;
        4) echo -e ${grnw} ${open}"You have choosen controller 4"${end} else;;
        5) echo -e ${grnw} ${open}"You have choosen controller 5"${end} else;;
        *) echo -e ${redw} ${open}"This is not a valid choice, please try again"${end}
        exit
        ;;
    esac
#END Validate

Item marked solved. thanks again for all your input.

pan64 02-10-2015 12:59 AM

glad to help you.
if you really want to say thanks just press YES

chrism01 02-10-2015 04:56 AM

That's why I put "continue here" - that's where your code would continue ....


All times are GMT -5. The time now is 08:27 PM.