1

I have a array with a few string values, and I correctly can out it in console:

for element in "${answers[@]}"
do echo $element
done

out:

1

2

5

But I need do some command for each value in array:

For example this implementation does not execute any action:

for element in "${answers[@]}";
do 

#    switch $element {
    case $element in
       1)
        echo "install app 1";;
       2)
        echo "install app 2";;
       3)
        echo "install app 3 ";;
esac 
done

In follow attempt:

for element in "${answers[@]}";
do

    switch $element {
    case "1": echo "install app 1";;
    case "2": echo "install app 2";;
    case "3": echo "install app 3";;
}
done

I have exception:

./prepare_2.0.sh: line 15: syntax error near unexpected token echo' ./prepare_2.0.sh: line 15: case "1": echo "install app 1";;'

How to correctly implement this logic?

  • 3
    You have a typo in the variable name in the case statement "elemant" – glenn jackman Oct 19 '18 at 22:17
  • @glennjackman Good catch! – WinEunuuchs2Unix Oct 19 '18 at 23:54
  • 1
    @glennjackman, yes, it is just typo. I did correct this and what's next? – Valentyn Hruzytskyi Oct 20 '18 at 15:47
1

You splits string value, but in first attempt you not use it. Just change '1)' to '"1")'

for element in "${answers[@]}"; do

case $element in
   "1")
    echo "install app 1";;
   "2")
    echo "install app 2";;
   "3")
    echo "install app 3 ";;
esac 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.