Skip to main content
Bash Scripting

Create Command Line Menus with Shell Scripting

By July 7, 2013September 12th, 2022No Comments

For experienced Linux users navigating the command line is not an issue, but if you have operators that need to spend some but not all their time at the command line they may appreciate the time you spend developing a menu. A menu written in shell scripting is not difficult but saves so much time for your operators and ensures their actions are repeatedly correct.

#!/bin/bash
trap '' 2  # ignore control + c
while true
do
  clear # clear screen for each loop of menu
  echo "================="
  echo "Menu ---"
  echo  "================="
  echo "Enter 1 to list users 1:"
  echo "Enter 2 for calendar 2:"
  echo "Enter q to quit q:"
  echo -e "Enter your selection here and hit <return> .. c"
  read answer  # create variable to retains the answer
  case "$answer" in
   1) who
      uptime;;
   2) cal ;;
   q) exit ;;
  esac
  echo -e "Hit the <return> key to continue.. c"
  read input ##This cause a pause so we can read the output
  #of the selection before the loop clear the screen
done

Running this script from the operators login script will cause the script to execute automatically for the user and if it is executed with

exec MENU.sh

Then when the exit statement is called from the quit section it will exit the script and log the user out, the video takes you through the process and demonstrates it use in a login script.