LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-30-2005, 01:10 AM   #1
lostinsyntax
LQ Newbie
 
Registered: Apr 2005
Posts: 5

Rep: Reputation: 0
Question Difficulties writing shell scripts


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
 
Old 04-30-2005, 07:34 AM   #2
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
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.

Last edited by jschiwal; 04-30-2005 at 07:37 AM.
 
Old 04-30-2005, 12:37 PM   #3
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 61
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.
 
Old 04-30-2005, 07:44 PM   #4
lostinsyntax
LQ Newbie
 
Registered: Apr 2005
Posts: 5

Original Poster
Rep: Reputation: 0
Thanks dudes/dudettes for the reply,

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.

Thanks,

lostinstntax
 
Old 04-30-2005, 09:12 PM   #5
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 61
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

Last edited by homey; 04-30-2005 at 11:13 PM.
 
Old 05-01-2005, 02:56 AM   #6
lostinsyntax
LQ Newbie
 
Registered: Apr 2005
Posts: 5

Original Poster
Rep: Reputation: 0
Smile

Thanks homey,

I'll give it a try. I apreciate you giving up your time like that to help me. If I get the opportunity I'll pass it on one day to someone else.

kindest regards,

lostinsyntax
 
Old 05-01-2005, 06:03 AM   #7
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 61
Thumbs up

Glad to help!
 
Old 05-02-2005, 12:58 AM   #8
lostinsyntax
LQ Newbie
 
Registered: Apr 2005
Posts: 5

Original Poster
Rep: Reputation: 0
Hey homey,

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.

Kindest regards,

lostinsyntax
 
Old 05-02-2005, 07:42 AM   #9
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 61
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Writing Shell Scripts in Redhat 6.1 KDE Ace Rimmer Linux - Newbie 5 08-16-2016 07:41 PM
Difficulties writing shell scripts lostinsyntax Linux - Newbie 2 05-01-2005 03:48 PM
I'm terrible at writing shell scripts. A little help fetching files? ashibaka Linux - General 6 04-10-2004 12:41 AM
Writing C++ Shell Scripts MasterKin8T Programming 5 09-29-2003 10:43 AM
writing Linux shell scripts in Windows NightWolf_NZ Linux - Newbie 3 09-10-2003 09:28 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:09 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration