Bash Script: System Menus

bash-scripts

We love bash scripts here at Societal Glitch. From simple shell script files to aliases, it’s all great. However, there are times when a menu system could be helpful even in the terminal. So that’s what we’ve done. This is our template for building a terminal-based menu system.

The menu is based on bash functions, commands, and some chaining. It features a hidden menu which is accessible from the main menu under the number “86”. Any of this can and should be changed and customized to your needs.

#!/bin/bash

# A terminal system menu to keep commonly used commands in a single location.
# Created 2023-6-9, Modified: 2023-11-17


function title (){

echo " ____________________________ "
echo "|  ________________________  |"
echo "| | MASTER CONTROL PROGRAM | |"
echo "| |________________________| |"
echo "|____________________________|"
echo "           $HOSTNAME          "
echo "                              "

}

function mcp (){

clear
title

while :
do

echo "*******************************"
echo " Hello $USER. Please make a selection."
echo " "
echo " 1) Menu 1 "
echo " 2) Menu 2 "
echo " 3) Menu 3"
echo " 4) Menu 4 "
echo " 5) "
echo " 6) "
echo " 99) Exit"
echo "*******************************"

read action
	case $action in

1) menu1;;
2) menu2;;
3) menu3;;
4) menu4;;
5) ;;
6) ;;
86) hidden ;;
99) exit ;;

esac
done
}

function menu1 (){

clear
title

while :
do

echo "-------------------------------"
echo "MCP: MENU 1"
echo "1) Install Updates"
echo "2) Edit Cronjobs"
echo "3) "
echo "4) "
echo "5) "
echo "6) "
echo "7) "
echo "99) Main Menu"
echo "-------------------------------"

read subaction
	case $subaction in
1) sudo apt update && sudo apt upgrade -y && sudo apt autoremove -y;;
2) crontab -e;;
3) ;;
4) ;;
5) ;;
6) ;;
7) ;;
99) mcp ;;

esac
done
}

function menu2 (){

clear
title

while :
do

echo "-------------------------------"
echo "MCP: MENU 2"
echo "1) "
echo "2) "
echo "3) "
echo "4) "
echo "5) "
echo "6) "
echo "7) "
echo "8) "
echo "9) "
echo "10) "
echo "99) Main Menu"
echo "-------------------------------"

read subaction
	case $subaction in
1)  ;;
2)  ;;
3)  ;;
4)  ;;
5)  ;;
6)  ;;
7)  ;;
8)  ;;
9)  ;;
10) ;;
99) mcp ;;

esac
done
}


function menu3 (){

clear
title

while :
do

echo "-------------------------------"
echo "MCP: MENU 3"
echo "1) "
echo "2) "
echo "3) "
echo "4) "
echo "5) "
echo "6) "
echo "7) "
echo "8) "
echo "9) "
echo "10) "
echo "99) Main Menu"
echo "-------------------------------"

read subaction
	case $subaction in
1)  ;;
2)  ;;
3)  ;;
4)  ;;
5)  ;;
6)  ;;
7)  ;;
8)  ;;
9)  ;;
10) ;;
99) mcp ;;

esac
done
}


function menu4 (){

clear
title

while :
do

echo "-------------------------------"
echo "MCP: MENU 4"
echo "1) "
echo "2) "
echo "3) "
echo "4) "
echo "5) "
echo "6) "
echo "7) "
echo "8) "
echo "9) "
echo "10) "
echo "99) Main Menu"
echo "-------------------------------"

read subaction
	case $subaction in
1)  ;;
2)  ;;
3)  ;;
4)  ;;
5)  ;;
6)  ;;
7)  ;;
8)  ;;
9)  ;;
10) ;;
99) mcp ;;

esac
done
}

function hidden (){

clear
title

while :
do

echo "-------------------------------"
echo "SECRET MENU"
echo "1) "
echo "2) "
echo "3) "
echo "4) "
echo "5) "
echo "6) "
echo "7) "
echo "8) Send MCP to bin"
echo "99) Main Menu"
echo "-------------------------------"

read subaction
	case $subaction in
1)  ;;
2)  ;;
3)  ;;
4)  ;;
5)  ;;
6)  ;;
7)  ;;
8)  ;;
99) mcp ;;

esac
done
}

mcp