Menu Driven Shell Script

by on April 5, 2008 · 8 comments

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

  1. #!/bin/bash
  2. # A menu driven Shell script which has following options
  3. # Contents of /etc/passwd
  4. # List of users currently logged
  5. # Prsent handling directory
  6. # Exit
  7. # As per option do the job
  8. # -----------------------------------------------
  9. # Copyright (c) 2005 nixCraft project <http://cyberciti.biz/fb/>
  10. # This script is licensed under GNU GPL version 2.0 or above
  11. # -------------------------------------------------------------------------
  12. # This script is part of nixCraft shell script collection (NSSC)
  13. # Visit http://bash.cyberciti.biz/ for more information.
  14. # -------------------------------------------------------------------------
  15.  
  16. while :
  17. do
  18. clear
  19. echo " M A I N - M E N U"
  20. echo "1. Contents of /etc/passwd"
  21. echo "2. List of users currently logged"
  22. echo "3. Prsent handling directory"
  23. echo "4. Exit"
  24. echo -n "Please enter option [1 - 4]"
  25. read opt
  26. case $opt in
  27. 1) echo "************ Conents of /etc/passwd *************";
  28. more /etc/passwd;;
  29. 2) echo "*********** List of users currently logged";
  30. who | more;;
  31. 3) echo "You are in $(pwd) directory";
  32. echo "Press [enter] key to continue. . .";
  33. read enterKey;;
  34. 4) echo "Bye $USER";
  35. exit 1;;
  36. *) echo "$opt is an invaild option. Please select option between 1-4 only";
  37. echo "Press [enter] key to continue. . .";
  38. read enterKey;;
  39. esac
  40. done


4000+ howtos and counting! If you enjoyed this article, join 45000+ others and get free email updates!

Click here to subscribe via email.

  • saravanan

    i want regularly script idea from you

  • al

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

  • Trevor

    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?

  • Mike Mestnik

    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.

  • Laszlo

    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.

  • Sudipto

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

  • anu

    its working,,try out using ubuntu

  • Polux

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

Previous Script:

Next Script: