ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I dont like to take up people's time but I have taken the below shell scripts as far as I can. I dont think I am capable of any more, and I guess I hate to say it but I need help. Thanks in advance for any assistance anyone can provide. Sorry to be burdonsome. PS: These have been written in vi text editor. PPS Posted this accidentally under Newbie forum.
Question
Write a shell script that will greet you with “Good Morning” if the time is between 12.00am to 11.59am, “Good Afternoon” if the time is between 12.00pm to 4.59pm, “Good Evening” if the time is between 5.00pm to 7.59pm and “Good Night” if it is between 8.00pm to 11.59pm.
Answer
greet=`date | cut –c12-13,15-16`;
if [ “$greet” => “0000” –a “$greet” <= “1159” ];
then
echo “Good Morning!”;
else
if [ “$greet” => “1200” –a “$greet” <= “1659” ];
then
echo “Good Afternoon!”;
else
if [ “$greet” => “1700” –a “$greet” <= “1959” ];
then
echo “Good Evening”;
else
echo “Good Night”
fi
fi
fi
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 filename and the name of the directory for the search. Your shell script should run continuously until the user enters 5 for exit.
Answer
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
What type of script is this?
If you had included a "swish-bang" line in the script, that would be obvious.
For example, if the first line is:
#!/bin/bash
Then it is obvious that this is a bash script. Not only to the reader, but also to the computer! Those magic characters is what *nix uses to determine which interpreter should execute the file. That way, your shell could be different then the script.
Unless you are dealing with base 60 numbers, you might want to convert the time to minutes since midnight.
For example:
minutes=$(( `date +%H`*60 + `date +%M` ))
Then check if minutes < 720.
If you use cut, you need a hyphen before the 'c' option.
Also, beware of evil characters that have special meanings to the shell. Since you don't have any variable expansion in the echo arguments, you could put the text in single quotes.
lostinsyntax,
The biggest problem that I see in your script is the editor you are using is making "smart quotes" instead of ordinary "straight quotes". That causes failure when trying to run the script.
That's a great idea to convert to minutes. As for the rest it kind of freaks me out as I dont understand. I'll take on board what you said but I think I need to do a lot more study up on it.
The part I mentioned about smart quotes ....
Try these examples in a terminal and see how differently they act.
Code:
echo “choice”
echo "choice"
The example on the bottom has straight quotes and they make your script work.
Here's a little putzing I did with your script....
Code:
#!/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
Your one clever dude/dudette. Your script runs like a charm. But I managed to get the greeting
script to work like a charm as well. You run your scripts straight from the shell whereas I save
them in vi editor (in case I make a mistake) and run then with the sh command. That confused me
a bit. Anyway check this out, bit proud of it but I dont want to blow my own horn.
set `date +%H`
echo $*
echo $1
if [ $1 -lt 12 ]
then
echo Good morning!
elif [ $1 -lt 17 ]
then
echo Good afternoon!
elif [ $1 -lt 20 ]
then
echo Good evening!
else
echo Good night!
fi
Runs well hey. Maybe one day I'll be able to match wits with you but I doubt it.
One must live in hope or what else is there.
Actually, if you look at my shebang ( !#/bin/bash ) you can see that I run them from a bash script.
I make it executable with the command: chmod +x test
and run it with the command: ./test
Don't get fooled by my examples into thinking that I automatically did it right the first time. I have to make several attempts and google for help just like anybody else. After I post the answer, I often think of some more improvements so the post ends up getting edited more times than you can believe.
Your time script looks great! Hopefully, you will learn from the pros as I am still an apprentice.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.