Wednesday, November 19, 2014

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
  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

0 comments: