≡ Menu

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

# 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 
# 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 :
 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;;
{ 8 comments… add one }
  • Sudipto January 28, 2011, 5:52 am

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

    • Polux December 19, 2011, 3:52 pm

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

  • Laszlo August 11, 2010, 11:08 am

    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 July 21, 2010, 5:43 pm

    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 July 3, 2009, 7:14 pm

    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 April 27, 2011, 3:05 pm

      its working,,try out using ubuntu

  • al July 2, 2009, 7:58 pm

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

  • saravanan August 22, 2008, 8:30 am

    i want regularly script idea from you

Security: Are you a robot or human?

Leave a Comment