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 |
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/
|
Here it is:
Code:
read -p "Controller ID#: " ctrl |
This is one way
Code:
#!/bin/bash |
WOW thanks all for you suggestions. I will try it out this afternoon and let mark the ticket solved.
|
@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 Code:
./test.sh: line 89: [: a: integer expression expected @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 |
you can do it without regexp:
Code:
case exp in $var |
@pan64
thanks your help I got the case function to correctly. Code:
# Validate User Input |
glad to help you.
if you really want to say thanks just press YES |
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. |