Scheduled Maintenance: We are aware of an issue with Google, AOL, and Yahoo services as email providers which are blocking new registrations. We are trying to fix the issue and we have several internal and external support tickets in process to resolve the issue. Please see: viewtopic.php?t=158230

 

 

 

Making a multi choice Menu using a bash script

Programming languages, Coding, Executables, Package Creation, and Scripting.
Post Reply
Message
Author
User avatar
GarryRicketson
Posts: 5644
Joined: 2015-01-20 22:16
Location: Durango, Mexico

Making a multi choice Menu using a bash script

#1 Post by GarryRicketson »

This is really my first attempt at doing some sort of "program" with just a bash script.
I notice so many packages, or program installers just use a "bash script" to do the job,
and "bash" is maybe the most powerful tool that comes with Debian, and other linux distros. So I decided to start trying to do something with it.
So this is my first project, been looking at some tutorials, and have lots of links to some pretty good ones. But any way, I am trying to learn about making a multi choice menu.
I found some examples in different tutorials,the example I used, just had 2 options, and neither really did anything.
It just:
1 . printed and displayed a "hello world", 2 . quit ... so I looked some more, and did figure out how to get option 1. start the w3m web browser, that works fine. I added options 1 thru 6 and it show 7 options, including the "quit" option.
Problem, no matter how I try to add the 2nd option, I get errors, I tried all sorts of places, and remove the lines, that the error message showed to be "error", still no, went back to the tutorial, but do not find anything I can understand, maybe some other tutorial, but any way, maybe someone can help get me going on this too,.. here is what I have so far.
The first part works great, when I select 1 it start the w3m browser, and connects here,
when I quit the browser, it does not return to the menu.
---edited:"it returns to the menu",..should have been:

script#1

Code: Select all

 #!/bin/bash
           OPTIONS="1 2 3 4 5 6 Quit"
           select opt in $OPTIONS; do
               if [ "$opt" = "Quit" ]; then
                echo done
                exit
               elif [ "$opt" = "1" ]; then
                 while w3m http://forums.debian.net
echo done
exit
                 do
              true
               done
               else
                clear
                echo bad option
               fi
           done
[/ code]
#---------------------------------
#This would be the "option 2 " but when I try to add it I get errors, seems like  no matter where I add it. 
 elif [ "$opt" = "2" ]; then
                 while display
                echo done
             exit  
It would then also include the options for 3 4 5 6 as well,
Option 2 is to start "imagemagick", "display" is the cli command to start it.
Maybe it does not use the "elif", so I tried "if" and that does not work ? anyway I got tired , I used to be able to do some menus pretty easy, but it was with "qbasic", and I have forgotten a lot of that, but any way "bash" is not the same,..
I also am going to look at

Code: Select all

 man bash 

I might find what I need there,..as busy as I am , it will be a while, doing this via the manual and tutorials, it is a simple enough script, I am hoping some one that knows bash scripting better, can give a example, to help speed this up. It is just a small piece, of another project I am starting. More on that later
thanks,
Last edited by GarryRicketson on 2015-09-19 14:20, edited 1 time in total.

User avatar
GarryRicketson
Posts: 5644
Joined: 2015-01-20 22:16
Location: Durango, Mexico

Re: Making a multi choice Menu using a bash script

#2 Post by GarryRicketson »

Seems like I am talking to my self,..ok well no matter...
After looking at another "tutorial", I found a better example, at least for me, this was much easier to work with, it is not a very pretty menu,.. but any way it works.
So I am kind of happy about that.
http://linuxcommand.org/wss0120.php is where I got the example,
in this one, it is very easy to add additional commands, and choices to the menu, as needed, and that is important to me. This one does return to the menu, when I quit the option.
Script#2

Code: Select all

 #!/bin/bash

selection=
until [ "$selection" = "0" ]; do
    echo ""
    echo "PROGRAM MENU"
    echo "1 - WebBrowser"
    echo "2 - Image Magick"
    echo "3 - Editor" 
    echo "4 -E-Mail"  
 echo ""
    echo "0 - SHUT DOWN "
    echo ""
    echo -n "Enter selection: "
    read selection
    echo ""
    case $selection in
        1 ) w3m http://forums.debian.net ;;
        2 ) display ;;
        3 ) pluma ;;
        4 ) w3m file:///home/therest/is/sensitive.html ;;
        0 ) exit ;;
        * ) echo "Please enter 1, 2, 3, 4 or 0"
    esac
done
            
It works good , after I start the script, I can do any of the 4 choices, when I exit one choice, it returns to the menu, The 0 option, right now is "exit", but later that will be the "shutdown" command, to turn off the computer, I don't want it to do that yet.
Guess that is about it for now,..
Last edited by GarryRicketson on 2015-09-19 14:22, edited 1 time in total.

BowCatShot
Posts: 959
Joined: 2006-07-15 12:08

Re: Making a multi choice Menu using a bash script

#3 Post by BowCatShot »

Here's one that I use for selecting folders to be included in a music playlist. The code isn't very clean since I wrote it in a hurry for my use only, but it works.

Code: Select all

#!/bin/bash

menuitems=" 0|all |off "

folders[0]="all"

linenumber=0

OK=0
CANCEL=1

# OLDIFS=$IFS
# IFS="|"
OLDFOLDER=$(pwd)

cd Music

for fn in *
do
 if [ -d "$fn" ]
 then
    ((linenumber++))

   menuitems="$menuitems|""$linenumber""|$fn"" |off "
   folders[$linenumber]="$fn"
 fi
done

#echo $menuitems
OLDIFS=$IFS
IFS="|"

selections=$(kdialog --checklist "Select Folders" $menuitems --geometry 400x300 | tr -d "\"")
#selections=$(zenity  --list  --width=400 --height=400 --text "Choose folders for playlists" --checklist  --column "Pick" --column "Folders" $menuitems --separator="|")
rc=$?

IFS=$OLDIFS

if [ $rc == $CANCEL ]
then
    exit
fi


tempfile=$(mktemp -p /tmp)
# echo $tempfile

cd $OLDFOLDER

for n in $selections
do

  if [ $n -eq 0 ]
  then

      find ./Music/ \( -name "*.wav" -o -name "*.mp3" \) -print | grep -v "REJECT" | grep -v "ding.wav" | grep -v "4minutedelay.wav" | cut -b 2- > $tempfile
      break

  fi

  find ./Music/"${folders[$n]}" \( -name "*.wav" -o -name "*.mp3" \) -print | grep -v "REJECT" | grep -v "ding.wav" | grep -v "4minutedelay.wav" | cut -b 2- >> $tempfile

done

cd $OLDFOLDER

cat $tempfile | shuf --random-source=/dev/urandom | shuf --random-source=/dev/urandom | shuf --random-source=/dev/urandom | Interleave | sed '1,1s/^/#EXTM3U\n/m' | tee MyList.m3u | sed '3,$s/^/4minutedelay.wav\n/m' > MyListWithdelay.m3u

for (( i=0; i<3; i++ ))
do
  echo ding.wav >> MyList.m3u
  echo ding.wav >> MyListWithdelay.m3u
done

rm $tempfile

User avatar
alansmithee
Posts: 41
Joined: 2013-02-02 08:02

Re: Making a multi choice Menu using a bash script

#4 Post by alansmithee »

GarryRicketson wrote: The first part works great, when I select 1 it start the w3m browser, and connects here,
when I quit the browser, it returns to the menu.
I actually don't understand this behavior. I'd expect the script to complete (exit) after you quit w3m, not return to your menu.

The reason you can not add more actions is (IIUC) because you are exiting the script as part of the while conditional test. Your consequent commands are never executed.

Code: Select all

while w3m http://forums.debian.net
      echo done
      exit
do
      true
done
'alansmithee' is the user formerly known as 'saulgoode'.

User avatar
kiyop
Posts: 3983
Joined: 2011-05-05 15:16
Location: Where persons without desire to improve themselves fear to tread, in Japan
Been thanked: 3 times

Re: Making a multi choice Menu using a bash script

#5 Post by kiyop »

"dialog" is useful besides "zenity".

I found "kdialog" in the package "kde-baseapps-bin": https://packages.debian.org/jessie/kde-baseapps-bin
by a search: https://packages.debian.org/search?suit ... ds=kdialog
In my debian, "kde-baseapps-bin" package is not installed.
Openbox, JWM: Jessie, Sid, Arch / Win XP (on VirtualBox), 10
http://kiyoandkei.bbs.fc2.com/

User avatar
GarryRicketson
Posts: 5644
Joined: 2015-01-20 22:16
Location: Durango, Mexico

Re: Making a multi choice Menu using a bash script

#6 Post by GarryRicketson »

GarryRicketson wrote:
The first part works great, when I select 1 it start the w3m browser, and connects here,
when I quit the browser, it returns to the menu.
I actually don't understand this behavior. I'd expect the script to complete (exit) after you quit w3m, not return to your menu.
Actually , I wrote that comment wrong, I am going to also edit, that post, Add script#1, so it is more clear, and on the "script#1",..that was another "problem" it exited, when I quit the w3m browser, ""it returns to the menu."" should be "it does not return to the menu"....
The second script, does,..that will be script#2,...

Thanks on this ....
The reason you can not add more actions is (IIUC) because you are exiting the script as part of the while conditional test ---snip---
Which is also why it exits the program, when I close the browser,..
I will also try the scripts you have shown, just to see how they work, there are all ways so many different ways to do something, and that is what I am doing at this point, trying to find the way that best suits me. thank you for the feedback
@by kiyop » Thank you, I just now see that, while I was typing,...

User avatar
kiyop
Posts: 3983
Joined: 2011-05-05 15:16
Location: Where persons without desire to improve themselves fear to tread, in Japan
Been thanked: 3 times

Re: Making a multi choice Menu using a bash script

#7 Post by kiyop »

GarryRicketson wrote:@by kiyop » Thank you, I just now see that, while I was typing,...
Not at all :)
Openbox, JWM: Jessie, Sid, Arch / Win XP (on VirtualBox), 10
http://kiyoandkei.bbs.fc2.com/

runfrodorun
Posts: 202
Joined: 2013-06-19 05:09

Re: Making a multi choice Menu using a bash script

#8 Post by runfrodorun »

@kiyop +1

I always use dialog for things like this. No need to reinvent the wheel. Debian installer uses it I believe.

-RJ
Much opinionated.
Some abrasive.
No systemd.
Wow.

Post Reply