I have a shell script in which I would like to prompt the user with a dialog box for inputs when the script is executed.
example (after script has started) :
"Enter the files you would like to install : "
user input : spreadsheet json diffTool
where $1 = spreadsheet, $2 = json, $3 = diffTool
then loop through each user input and do something like
for var in "$@"
do
echo "input is : $var"
done
How would I go about doing this within my shell script?
Thank you in advance
You need to use the
read
built-in available inbash
and store the multiple user inputs into variables,Give your inputs separated by space. For example, when running the above,
now each of the above inputs are available in the variables
arg1
,arg2
andarg3
The above part answers your question in way, you can enter the user input in one go space separated, but if you are interested in reading multiple in a loop, with multiple prompts, here is how you do it in
bash
shell. The logic below get user input until the Enter key is pressed,Now all your user inputs are stored in the array
inputArray
which you can loop over to read the values. To print them all in one shot, doOr a more proper loop would be to
and access individual elements as
"${inputArray[0]}"
,"${inputArray[1]}"
and so on.