I need to make a bash script that takes user input and does some validation.
The input is small. I just need to get a user choice “yes” or “no” and a file path and make sure the input is indeed a filepath and that he entered yes or no.
How can I validate the user input?
from your bash prompt type
help read
example:
step 1.
Yes or No :Yes
Yes
step 2.
ANSWER is yes
Or all on one line:
ANSWER is yes
So something vaguely like::
The test command is your friend. You really need to learn its ways ASAP.
help [and
help testThe “help bashthing” command is a great quick reminder.
see also: the Advanced Bash Scripting Guide (ABS)
http://tldp.org/LDP/abs/html/
FWIW:
This is a very simple example and wont accommodate much in the way of unexpected user input.
I would probably call functions for each step of this as then I could conditionally represent the user with another chance.
The case tests nested like this is plain ugly 🙂 === hard to get neat and read later
There are other ways to achieve all this try zenity for a GTK style GUI.
The A.B.S is excellent but dont try to eat it all at once !