Hey guys. I have two possible answers to this question. I dont think both work but im hoping someone can point out which way is best to use and which will work.
Question:
Write a shell script which will display a menu as follows
and accepts an input key from the user to execute an option
on the menu.
My Menu
~~~~~~~
1) Display today's date and system uptime
2) Display calendar for a particular month and year
3) Display current logon users
4) Find a file
5) Exit from menu
Please enter your choice (1-5):
For option 2, the user must be allowed to enter the month
and the year for calendar display. For option 4, you must
use the find command to find the file and the user must be
allowed to enter the file name and the name of the directory
for the search. Your shell script should run continuously
until the user enters 5 for exit.
Answer 1:
tput clear;
tput cup 3 17; echo “My Menu”;
tput cup 4 17; echo “~~~~~~~”;
tput cup 6 12; echo “1) Display today’s date and system uptime”;
tput cup 7 12; echo “2) Display calendar for a particular month and year”;
tput cup 8 12; echo “3) Display current logon users”;
tput cup 9 12; echo “4) Find a file”;
tput cup 10 12; echo “5) Exit from menu”;
tput cup 11 17; echo “Please enter your choice (1-5):”; tput cup 11 49;
read choice
if [ “$choice” = “1” ];
then
tput cup 12 17; date
else
if [ “$choice” = “2” ];
then
tput cup 12 17; echo “Please enter month:”; tput cup 12 37;
read month
tput cup 13 17; echo “Please enter year:”; tput cup 13 37;
read year
cal
else
if [ “$choice” = “3” ];
then
tput cup 12 17; who
else
if [ “$choice” = “4” ];
then
tput cup 12 17; echo “What is the name of the directory?”; tput cup 12 52;
read directory
tput cup 13 17; echo “What is the filename?”; tput cup 13 52;
read filename
tput cup 14 17; find /directory –name filename
else
exit
fi
fi
fi
fi
Answer 2:
#!/bin/bash
while true ; do
clear
tput cup 3 32; echo "My Menu"
tput cup 4 12; echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~"
tput cup 6 12; echo "1. Display today’s date and system uptime"
tput cup 7 12; echo "2. Display calendar for a particular month and year"
tput cup 8 12; echo "3. Display current logon users"
tput cup 9 12; echo "4. Find a file"
tput cup 10 12; echo "5. Exit from menu"
tput cup 11 17; echo -n "Please enter your choice (1-5): "
read sel
case $sel in
1) tput cup 13 17; echo -e "Todays date is `date`"
tput cup 14 17; echo -e "Uptime is `uptime |awk '{print$3}'`\n\n" ;;
2) tput cup 13 17; echo -n "Please enter month: " ;
read month
tput cup 14 17; echo -n "Please enter year (4 digits): " ;
read year
echo -e "\n\n`cal $month $year`\n\n" ;;
3) tput cup 13 17; who ;;
4) tput cup 13 17; echo -n "Name of the directory? " ;
read directory
tput cup 14 17; echo -n "What is the filename? " ;
read filename
tput cup 16 17; find $directory/$filename ;;
5) exit ;;
esac
sleep 10
done
--------------------
The UNIX software used at uni is called SSH
I would greatly appreciate if anyone could just let me know where i have gone wrong, what it should look like and which way is best.
Thank you in advanced. Took me hours to do this.