1

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"?

5
  • If you're using bash 4.x, it has associative arrays. That would be better than indirect variables like this. Commented May 10, 2014 at 1:22
  • Could you explain what exactly do you mean ? Commented May 10, 2014 at 1:29
  • An associative array is an array that uses strings as indexes, instead of just numbers. So you can have ${answer[${instal_list[$i]}]}. Commented May 10, 2014 at 1:30
  • I think that I understand now. So I would have to declare two arrays at the beginning ( declare -a instal_list and declare -A answer_ ) ? Commented May 10, 2014 at 1:42
  • So my IF should look like this : if [[ ${answer_[${instal_list[i]}]} == "yes" ]] || [[ ${answer_[${instal_list[i]}]} == "Yes" ]] || [[ ${answer_[${instal_list[i]}]} == "YES" ]]; Commented May 10, 2014 at 1:55

2 Answers 2

1
thing="Gimp"
answer_Gimp="Yes"

variableName="answer_$thing"
echo "The value of $variableName is ${!variableName}"
Sign up to request clarification or add additional context in comments.

Comments

0

If you have bash version 4, you can use an associative array:

declare -a instal_list=("Gimp" "VLC" "Gedit")
declare -a to_install
declare -A answers

for prog in "${instal_list[@]}"; do
    read -p "Do you want to install $prog? [y/n] " answer["$prog"]
    if [[ "${answer["$prog"],,}" == y* ]]; then
        to_install+=( "$prog" )
    fi
done

echo you want to install:
printf "   %s\n" "${to_install[@]}"

Looking at that, I don't see why you need to keep the answers in an array. But if you do, that's how you'd do it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.