Menu

From wiki
Revision as of 19:59, 28 August 2020 by Hdridder (talk | contribs) (Created page with "Category:Bash Category:Linux/Unix Super simple bash menu ([https://bash.cyberciti.biz/guide/Menu_driven_scripts inspiration]) <syntaxhighlight lang=bash> #!/bin/bash #...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Super simple bash menu (inspiration)

#!/bin/bash
# Super simple bash menu 
## ----------------------------------
# Step #1: Define variables
# ----------------------------------
RED='\033[0;41;30m'
STD='\033[0;0;39m'
 
# ----------------------------------
# Step #2: User defined function
# ----------------------------------
pause(){
  read -p "Press [Enter] key to continue..." fackEnterKey
}

one(){
  echo "one() called"
  pause
}
 
# do something in two()
two(){
  echo "two() called"
  pause
}
 
# function to display menus
show_menus() {
  clear
  echo "~~~~~~~~~~~~~~~~~~~~~"	
  echo " M A I N - M E N U"
  echo "~~~~~~~~~~~~~~~~~~~~~"
  echo "1. do one "
  echo "2. do two"
  echo "3. do three"

  echo "9. Exit"
}
# read input from the keyboard and take a action
#  call a function defined above or a script anywhere on your system
read_options(){
    local choice
    read -p "Enter choice [ 1 - 3] " choice
    case $choice in
        1) one ;;
	2) two ;;
	3) <pathtoacommand>
           pause ;;
	9) exit 0;;
	*) echo -e "${RED}Error...${STD}" && sleep 2
    esac
}
 
# ----------------------------------------------
# Step #3: Trap CTRL+C, CTRL+Z and quit singles
# ----------------------------------------------
trap '' SIGINT SIGQUIT SIGTSTP
 
# -----------------------------------
# Step #4: Main logic - infinite loop
# ------------------------------------
while true
do
  show_menus
  read_options
done