Welcome to DU! The truly grassroots left-of-center political community where regular people, not algorithms, drive the discussions and set the standards. Join the community: Create a free account Support DU (and get rid of ads!): Become a Star Member Latest Breaking News Editorials & Other Articles General Discussion The DU Lounge All Forums Issue Forums Culture Forums Alliance Forums Region Forums Support Forums Help & Search

Computer Help and Support

In reply to the discussion: howid. [View all]

usonian

(14,364 posts)
2. Keep it simple.
Sat May 4, 2024, 08:20 PM
May 2024

I copied the bash script from this site:

https://askubuntu.com/questions/1705/how-can-i-create-a-select-menu-in-a-shell-script
I INSERTED SPACES TO KEEP DU FROM DISPLAYING SMILIES!!!
NEVER PUT A CLOSE PAREN AFTER A DOUBLE QUOTE.
YOU GET THIS &quot
And DU doesn't honor indents. It's not a code site.

Question:
I'm creating a simple bash script and I want to create a select menu in it, like this:

$./script

echo "Choose your option:"

1) Option 1
2) Option 2
3) Option 3
4) Quit

Answer 1

#!/bin/bash
# Bash Menu Script Example

PS3='Please enter your choice: '
options=("Option 1" "Option 2" "Option 3" "Quit" )
select opt in "${options[@]}"
do
case $opt in
"Option 1" )
echo "you chose choice 1"
;;
"Option 2" )
echo "you chose choice 2"
;;
"Option 3" )
echo "you chose choice $REPLY which is $opt"
;;
"Quit" )
break
;;
*) echo "invalid option $REPLY";;
esac
done


Looks easy enough.

Mac version (tested and works)

#!/bin/bash
# Bash Menu Script Example

PS3='Please enter your choice: '
options=("Clock" "Notes" "Stickies" "Quit" )
select opt in "${options[@]}"
do
case $opt in
"Clock" )
open -a Clock &
;;
"Notes" )
open -a Notes &
;;
"Stickies" )
open -a Stickies &
;;
"Quit" )
break
;;
*) echo "invalid option $REPLY";;
esac
done


It works with the space between the " and the )
And prints on DU
One could get fancier. I didn't even read the other answers.
Bash is essential to computer use!
You BASH the computer or the computer BASHES you.

For anything more deluxe, use Tcl and Tk.
They are interpreted and have a GUI toolkit (Tk)
Active State has an installer. Free
https://www.activestate.com/products/tcl/

Note on the above.
I put that code into a script, called it do
chmod +x do
./do

Anyway, here's a screenshot FYI to show the indentation (for looks in Bash. Indentation counts in Python)

Recommendations

0 members have recommended this reply (displayed in chronological order):

Latest Discussions»Help & Search»Computer Help and Support»howid.»Reply #2