0
#!/bin/bash

PS3="Select an install package: "
items=("Essentials - VLC"
       "Graphics Suite - Inkscape, GIMP, Blender"
       "Streaming Suite - OBS, Openshot, Blender")
select opt in "${items[@]}" "Cancel - Install Manually"
do
    case $opt in
        1) echo "== Installing Essentials ==";
           sudo apt install vlc -y; wait; break;;
        2) echo "== Installing Graphics Suite ==";
           sudo apt install vlc inkscape gimp blender -y; wait; break;;
        3) echo "== Installing Streaming Suite ==";
           sudo apt install vlc obs openshot blender -y; wait; break;;
        $((${#items[@]}+1)))
           echo "Remember, you can manually install with";
           echo "  'sudo apt install [app name]'";
           echo "  'sudo flatpak install [app name]'";
           echo "or through the 'Software Manager'";
           sleep 8; break;;
        *) echo "Invalid option - please select a number from 1 to $((${#items[@]}+1))";
    esac
done
clear

I'm experimenting with an item select command. When I open the script as an executable and it opens a terminal window (top example), the script runs as expected. But when I open the Terminal and run the script with a sh command (bottom example), I get an "(" unexpected syntax error on line 42 and the script breaks.

What's going on here?

# Line 42 lands on the options tab.

options=("Essentials - VLC"\
         "Graphics Suite - Inkscape, GIMP, Blender"\
         "Streaming Suite - OBS, Openshot, Blender")

I also tried it on a single line (without the backslashes) but got the same error result when I tried to run it from the Terminal with sh.

EDIT: I tried running it with bash and it ran without breaking, but now it gives me the else condition: *) echo "Invalid option... no matter which option I pick.

7
  • 2
    You wrote your script for bash, so it must be run by bash and not by sh. Commented Sep 13, 2023 at 6:27
  • Okay. This time I used the 'bash' command to run it and that pesky error didn't reappear, but now no matter which number I input, I always get the else "*)" condition: "Invalid option - select..." Commented Sep 13, 2023 at 16:41
  • Show the code (not a screenshot). Show how you call the script. Post the complete error message. Commented Sep 14, 2023 at 10:02
  • Screenshot removed; relevant code added Commented Sep 15, 2023 at 15:41
  • Add declare -p opt >&2 to log what value opt has before you enter your case. Commented Sep 15, 2023 at 15:46

1 Answer 1

1

sh aka POSIX shell, don't have array feature. Choose if you want to use or .

bash support array and even, associative arrays:

#!/bin/bash

options=(
    "Essentials - VLC"
    "Graphics Suite - Inkscape, GIMP, Blender"
    "Streaming Suite - OBS, Openshot, Blender"
)

select x in "${options[@]}"; do
    echo "$x"
done

1) Essentials - VLC
2) Graphics Suite - Inkscape, GIMP, Blender
3) Streaming Suite - OBS, Openshot, Blender
#? 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.