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

AllaN01Bear

(23,039 posts)
Sat May 4, 2024, 06:22 PM May 2024

howid.

i have been looking something for either bash or python or mac terminal menu
i want it to look like this ,,,
1 : firefox
2: bean.
3 skype
4 exit.
you get my drift.
why, because i want to.
found many examples online but many are full of errors and i dont trust them
i have given up for now.

11 replies = new reply since forum marked as read
Highlight: NoneDon't highlight anything 5 newestHighlight 5 most recent replies

AuntyGravity

(289 posts)
1. Consider perhaps an AI code generator?
Sat May 4, 2024, 06:51 PM
May 2024

Here's a list!

https://www.unite.ai/best-ai-code-generators/

I'd be interested in your results if you decide to experiment with this interesting tech.
Useful or hooey?

usonian

(13,782 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)

AllaN01Bear

(23,039 posts)
5. i saved the mac version to my word processer . this is exactly what i am looking for . thank you for your
Sat May 4, 2024, 08:35 PM
May 2024

information and timely research. will play with it more when i get home from church. now off to bed .

usonian

(13,782 posts)
6. I did sysadmin and various scripting for a living.
Sat May 4, 2024, 08:51 PM
May 2024

Just had to brush up. It has been a long while.

On a mac, applescript is probably best. (Automator is applescript with a GUI and recorder)

On the mac, the terminal loses focus, so you have to click on its window to re-activate it.

Good luck.

usonian

(13,782 posts)
8. I just found this "cheat sheet" of sorts.
Mon May 6, 2024, 12:38 PM
May 2024

It doesn't really go into detail.
Perhaps the O'Reilly free book I linked might be better. Haven't read it in a while.

https://github.com/jlevy/the-art-of-command-line?tab=readme-ov-file#meta

Latest Discussions»Help & Search»Computer Help and Support»howid.