Bash: Menu Seed Script

 

Simple seed script for creating a menu system on linux( from stackoverflow )

#!/bin/bash
# Menu Runner

PS3='Please enter choice: '
opts=("ls" "pwd" "opt3" "Quit")
select opt in "${opts[@]}"
do
    case $opt in
        "ls")
            echo "u chose ls"
            ;;
        "pwd")
            echo "u chose pwd"
            ;;
        "opt3")
            echo "u typed '$REPLY' which calls '$opt'"
            ;;
        "Quit")
            break
            ;;
            *) echo "invalid option $REPLY";;
    esac
done

 


More complex implementation

title="Select example"
prompt="Pick an option:"
options=("A" "B" "C")

echo "$title"
PS3="$prompt "
select opt in "${options[@]}" "Quit"; do 

    case "$REPLY" in

    1 ) echo "You picked $opt which is option $REPLY";;
    2 ) echo "You picked $opt which is option $REPLY";;
    3 ) echo "You picked $opt which is option $REPLY";;

    $(( ${#options[@]}+1 )) ) echo "Goodbye!"; break;;
    *) echo "Invalid option. Try another one.";continue;;

    esac

done

while opt=$(zenity --title="$title" --text="$prompt" --list \
                   --column="Options" "${options[@]}"); do

    case "$opt" in
    "${options[0]}" ) zenity --info --text="You picked $opt, option 1";;
    "${options[1]}" ) zenity --info --text="You picked $opt, option 2";;
    "${options[2]}" ) zenity --info --text="You picked $opt, option 3";;
    *) zenity --error --text="Invalid option. Try another one.";;
    esac

done

Shell GUI Version

#!/bin/bash

HEIGHT=15
WIDTH=40
CHOICE_HEIGHT=4
BACKTITLE="Backtitle here"
TITLE="Title here"
MENU="Choose one of the following options:"

OPTIONS=(1 "Option 1"
         2 "Option 2"
         3 "Option 3")

CHOICE=$(dialog --clear \
                --backtitle "$BACKTITLE" \
                --title "$TITLE" \
                --menu "$MENU" \
                $HEIGHT $WIDTH $CHOICE_HEIGHT \
                "${OPTIONS[@]}" \
                2>&1 >/dev/tty)

clear
case $CHOICE in
        1)
            echo "You chose Option 1"
            ;;
        2)
            echo "You chose Option 2"
            ;;
        3)
            echo "You chose Option 3"
            ;;
esac

 


 

Using Functions

# USING FUNCTIONS
# ---------------
function demoFunction {
        read -n1 -p "Pick a letter to run a command [A, B, or C for more info] " runCommand

        case $runCommand in
            a|A) printf "\n\tpwd being executed...\n" && pwd;;
            b|B) printf "\n\tls being executed...\n" && ls;;
            c|C) printf "\n\toption A runs pwd, option B runs ls\n" && demoFunction;;
        esac
}

demoFunction

 


Another Example

!/usr/bin/env bash

echo "Main Menu"

# Define the choices to present to the user, which will be
# presented line by line, prefixed by a sequential number
# (E.g., '1) copy', ...)
choices=( 'copy' 'exit' )

# Present the choices.
# The user chooses by entering the *number* before the desired choice.
select choice in "${choices[@]}"; do

  # If an invalid number was chosen, $choice will be empty.
  # Report an error and prompt again.
  [[ -n $choice ]] || { echo "Invalid choice." >&2; continue; }

  # Examine the choice.
  # Note that it is the choice string itself, not its number
  # that is reported in $choice.
  case $choice in
    copy)
      echo "Copying..."
      # Set flag here, or call function, ...
      ;;
    exit)
      echo "Exiting. "
      exit 0
  esac

  # Getting here means that a valid choice was made,
  # so break out of the select statement and continue below,
  # if desired.
  # Note that without an explicit break (or exit) statement,
  # bash will continue to prompt.
  break

done

 


while/select loop

#!/bin/bash

## array of menu entries
entries=( "for Copy"
          "for exit" )

## set prompt for select menu
PS3='Selection: '

while [ "$menu" != 1 ]; do                ## outer loop redraws menu each time
    printf "\nMain Menu:\n\n"             ## heading for menu
    select choice in "${entries[@]}"; do  ## select displays choices in array
        case "$choice" in                 ## case responds to choice
            "for Copy" )
                echo "CopyNUM"
                break                     ## break returns control to outer loop
                ;;
            "for exit" )         
                echo "Haiiii, exiting"
                menu=1                    ## variable setting exit condition
                break
                ;;
            * )
                echo "ssss"
                break
                ;;
        esac
    done
done

exit 0

Menu using Functions (+pause() Function )

 

#!/bin/bash
# Menu Runner
# ---------------
function menuShow(){
    PS3='Please enter choice: '
    opts=("ls" "pwd" "opt3" "ytdlphp" "Quit")
    select opt in "${opts[@]}"
    do
        case $opt in
            "ls")
                echo "u chose ls"
                ls
                ;;
            "pwd")
                echo "u chose pwd"
                pwd
                ;;
            "opt3")
                echo "u typed '$REPLY' which calls '$opt'"
                ;;
            "ytdlphp")
                cd ~/git/phpunitwebdriver
                #~/git/phpunitwebdriver/bin/phpunit --filter YtdlShellExecTest::getVideosArray
                ~/git/phpunitwebdriver/bin/phpunit --filter YtdlShellExecTest::readFromFileAndDownload

                mv *.mp4 ~/Videos
                mv *.webm ~/Videos
                ls -la ~/Videos
                pause
                ;;
            "Quit")
                break
                ;;
                *) echo "invalid option $REPLY";;
        esac
        pause 'Press [Enter] key to continue...'
    done
}

function pause(){
   read -p "$*"
}

menuShow

 

 

 

 

 

Tags