Menu Driven Shell Script

Explains how to write a menu driven Shell script, which has following options:
1. Contents of /etc/passwd
2. List of users currently logged
3. Present handling directory (working directory)
4. Exit

Take action as per selected option

#!/bin/bash
# A menu driven Shell script which has following options
# Contents of /etc/passwd
# List of users currently logged
# Prsent handling directory
# Exit
# As per option do the job
# -----------------------------------------------
# Copyright (c) 2005 nixCraft project <http://cyberciti.biz/fb/>
# This script is licensed under GNU GPL version 2.0 or above
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit http://bash.cyberciti.biz/ for more information.
# -------------------------------------------------------------------------
 
while :
do
 clear
 echo "   M A I N - M E N U"
 echo "1. Contents of /etc/passwd"
 echo "2. List of users currently logged"
 echo "3. Prsent handling directory"
 echo "4. Exit"
 echo -n "Please enter option [1 - 4]"
 read opt
 case $opt in
  1) echo "************ Conents of /etc/passwd *************";
     more /etc/passwd;;
  2) echo "*********** List of users currently logged";
     who | more;;
  3) echo "You are in $(pwd) directory";   
     echo "Press [enter] key to continue. . .";
     read enterKey;;
  4) echo "Bye $USER";
     exit 1;;
  *) echo "$opt is an invaild option. Please select option between 1-4 only";
     echo "Press [enter] key to continue. . .";
     read enterKey;;
esac
done
Get the latest tutorials on SysAdmin, Linux/Unix, Open Source, and DevOps topics:
CategoryList of Unix and Linux commands
File Management cat
Firewall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilities dig host ip nmap
OpenVPN CentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Manager apk apt
Processes Management bg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searching grep whereis which
User Information groups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPN CentOS 8 Debian 10 Firewall Ubuntu 20.04
8 comments… add one
  • Sudipto Jan 28, 2011 @ 5:52

    write a shell script to find how many different kind of files present in current directory

    • Polux Dec 19, 2011 @ 15:52

      ls -R | grep “\.” | grep -v “:” | cut -d”.” -f2 | sort | uniq
      Something like that ?

  • Laszlo Aug 11, 2010 @ 11:08

    A tip: using ” read -n 1 opt” line instead of ” read opt” in the script, the user should not press after the number selection, the corresponding operation immediately starts.

  • Mike Mestnik Jul 21, 2010 @ 17:43

    There are several programs that do this, but currently I can only find pdmenu and can not get it to function on CentOS. cursel is another on Freshmeat, but yum and apt say it dosen’t exist.

    pdmenu works Great! Thank you Joey Hess!
    Debian packages available.

  • Trevor Jul 3, 2009 @ 19:14

    This script does not seem to be working, can you advise on the following error:

    syntax error near unexpected token ‘in’
    error on line 12

    P.S. I shortened the script a little, but basically its the line starting with case $opt in which seems to be broken?

    I am running Solaris 10, does this make a difference?

    • anu Apr 27, 2011 @ 15:05

      its working,,try out using ubuntu

  • al Jul 2, 2009 @ 19:58

    Does anyone know how to select multiple choices at once instead of one at a time?

  • saravanan Aug 22, 2008 @ 8:30

    i want regularly script idea from you

Leave a Reply

Your email address will not be published. Required fields are marked *

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.