Skip to content

Instantly share code, notes, and snippets.

@sany2k8
Created February 9, 2017 05:43
Show Gist options
  • Save sany2k8/a3d8e4c390bde22b2abbf009da8b8376 to your computer and use it in GitHub Desktop.
Save sany2k8/a3d8e4c390bde22b2abbf009da8b8376 to your computer and use it in GitHub Desktop.
Menu in Bash
#!/bin/bash
# A menu driven shell script sample template
## ----------------------------------
# Step #1: Define variables
# ----------------------------------
EDITOR=vim
PASSWD=/etc/passwd
RED='\033[0;41;30m'
STD='\033[0;0;39m'
# ----------------------------------
# Step #2: User defined function
# ----------------------------------
one(){
echo 'one() function called'
pause
}
two(){
echo 'two() function called'
pause
}
pause(){
read -p "Press [ENTER] key to continue...." fackEnterKey
}
show_menu(){
clear
echo '~~~~~~~~~~~~~~~~~~~~~~~~~~~~'
echo '~~~~~~~~~~MAIN-MENU~~~~~~~~~'
echo '~~~~~~~~~~~~~~~~~~~~~~~~~~~~'
echo '1 .Set Terminal'
echo '2. Reset Terminal'
echo '3 .Exit Terminal'
}
read_options(){
local choice
read -p "Enter your choice [1-3] : " choice
case $choice in
1) one;;
2) two;;
3) 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_menu
read_options
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment