Navigation Menu

Currently the navigation bar is not functioning. Use the Blog Archive or the Google Custom Search to find content.

Saturday 22 June 2013

Shell scripting using general-purpose utilities - Menu driven program

Given below is a menu driven shell script which will print the following menu and execute the given task to display result on standard output.
MENU
1 Display calendar of current month
2 Display today’s date and time
3 Display usernames those are currently logged in the system
4 Display current directory
5 Display your terminal number
6 Exit

Links that you may find important :

Calender [cal] :

http://linux.about.com/library/cmd/blcmdl1_cal.htm http://www.hscripts.com/tutorials/linux-commands/cal.html http://www.computerhope.com/unix/ucal.htm

Date [date] :

http://linux.about.com/od/commands/l/blcmdl1_date.htm http://www.computerhope.com/unix/udate.htm

Who is logged on [who] :

http://linux.about.com/library/cmd/blcmdl1_who.htm
http://www.computerhope.com/unix/uwho.htm
https://www.ibm.com/developerworks/community/blogs/58e72888-6340-46ac-b488-d31aa4058e9c/entry/linux_who_command_with_examples61?lang=en

Current working directory [pwd] :

http://linux.about.com/library/cmd/blcmdl1_pwd.htm
http://linux.die.net/man/1/pwd

Terminal [tty] :

http://linux.about.com/od/commands/l/blcmdl4_tty.htm
http://linux.die.net/man/1/tty

Code :
echo "Menu"
echo "Press 1 to display calendar of current month" 
echo "Press 2 to display todays date and time"
echo "Press 3 to display usernames that are currently logged into the system"
echo "Press 4 to display current directory"
echo "Press 5 to display your terminal number"
echo "Press 6 to Exit"
echo "Enter your choice : "
read n
case $n in
1)
cal;;
2)
date;;
3)
who;;
4)
pwd;;
5)
tty;;
6)
exit;;
esac
Output :
Menu
Press 1 to display calendar of current month
Press 2 to display todays date and time
Press 3 to display usernames that are currently logged into the system
Press 4 to display current directory
Press 5 to display your terminal number
Press 6 to Exit
Enter your choice : 
1
   February 2012      
Su  Mo Tu  We Th  Fr  Sa  
                 1    2    3    4  
 5   6   7     8    9   10  11  
12  13  14  15  16  17  18  
19  20  21  22  23  24  25   
26  27  28  29           
                  
Menu
Press 1 to display calender of current month
Press 2 to display todays date and time
Press 3 to display usernames that are currently logged into the system
Press 4 to display current directory
Press 5 to deisplay your terminal number
Press 6 to Exit
Enter your choice : 
2
Mon Feb 20 10:56:40 IST 2012

Menu
Press 1 to display calender of current month
Press 2 to display todays date and time
Press 3 to display usernames that are currently logged into the system
Press 4 to display current directory
Press 5 to deisplay your terminal number
Press 6 to Exit
Enter your choice : 
3
john     pts/0        2012-02-20 10:28 (:0.0)

Menu
Press 1 to display calendar of current month
Press 2 to display todays date and time
Press 3 to display usernames that are currently logged into the system
Press 4 to display current directory
Press 5 to displays your terminal number
Press 6 to Exit
Enter your choice : 
4
/home/john/Linux/Prac3

Menu
Press 1 to display calender of current month
Press 2 to display todays date and time
Press 3 to display usernames that are currently logged into the system
Press 4 to display current directory
Press 5 to deisplay your terminal number
Press 6 to Exit
Enter your choice : 
5
/dev/pts/0
Note : Leave a comment if you feel the program is incorrect and/or has errors and/or if the program and its output don't match. Please report about broken links.

No comments:

Post a Comment