I’m trying to learn a few things about scripting and Linux systems, so I started learning bash scripting.
For an exercise, I’m trying to program a script that would install all programs that user chooses.
I worked out a skeleton of installation part, but I’m stuck at determining users answer.
Here is my code:
#!/bin/bash
declare -a instal_list=("Gimp" "VLC" "Gedit")
for ((i=0; i<3; i++))
do
echo "Do you want to install ${instal_list[i]} ?"
echo
read answer_${instal_list[i]}
if [[ $answer_${instal_list[i]} == "yes" ]] || [[ $answer_${instal_list[i]} == "Yes" ]] || [[ $answer_${instal_list[i]} == "YES" ]];
then
instal+=" ${install_list[i]}"
fi
done
My problem is in my if statement. Inside of it, I’m trying to evaluate if users answer is yes. Problem is in answer_${instal_list[i]} variable.
I don't know how to explain what I mean, so I will try to explain it on example.
Example :
- We run the script, and the script asks us if we want install Gimp.
- We say yes, and the script stores that answer in variable "answer_${instal_list[1]}" ("answer_Gimp").
- My problem is when I try to call back that variable ("answer_Gimp"). = To call it I use "$answer_${instal_list[1]}" line, but the shell doesn’t recognize that command as answer_Gimp.
So how can I call back variable answer_${instal_list[1]} so that shell would recognize it as "answer_Gimp"?
${answer[${instal_list[$i]}]}.declare -a instal_listanddeclare -A answer_) ?IFshould look like this :if [[ ${answer_[${instal_list[i]}]} == "yes" ]] || [[ ${answer_[${instal_list[i]}]} == "Yes" ]] || [[ ${answer_[${instal_list[i]}]} == "YES" ]];