Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

"Simple Prog Exercise" difficult for me

Status
Not open for further replies.

blinky85

Technical User
Jun 7, 2008
2
US
I am supposed to write a file called greetings that uses the cut command to calculate the hour of day, and after it calculates that I am supposed to display a message like "good morning" good afternoon, or good evening. After running the program I get an error:


jawaibel@cis:~$ bash greettings


21
greettings: line 10: [: missing `]'
greettings: line 13: [: missing `]'
good evening

jawaibel@cis:~$ vi greetttings
jawaibel@cis:~$ vi greettings
1 #
2 # greetings
3 # greetings program version 1
4 # A sample program using the if - then -elif construct
5 # This program displays greetings according to the time of day
6 #
7 echo
8 echo
9 hour= date | cut -c12-13
10 if ["$hour" -le 10]
11 then
12 echo good morning
13 elif ["$hour" -le 12]
14 then
15 echo good afternoon
16 else
17 echo good evening
18 fi
19 echo
20 exit 0
~
~
~
"greettings" 20L, 329C 10,1 All

any help is greatly appreciated :)
 
Nevermind Figured it out forgot ` in the hour part
 
Well, in fact no student posting allowed on this site...

Still, here are some hints (if you read 'em before you hand in the assignment):

- you may want to name your script greetings, not greettings. The exercise says "a file called greetings". A nitpicking teacher might deduct a point...

- use indentation, it eases reading programming constructs like if-then-elif-else-fi and especially teachers like this ;-).
[tt]
if [ $hour -lt ...
then
echo ...
elif [ $hour -lt ...
then
echo ...
else
echo ...
fi
[/tt]
- Think again about your changeover-hours?

Good luck!


HTH,

p5wizard
 
I realize part of the assignment was to use [tt]cut[/tt], but this is an easier way to get the hour...
Code:
# For Korn Shell
date '+%H' | read hour

# For Bourne Shell or BASH
hour=`date '+%H'`
Check the [tt]man[/tt] pages for [tt]date[/tt] for more info.
Code:
man date

 
I'd recommend getting a good shell scripting book.

I have (now out of print) UNIX Shells by Example. It's fantastic. Poke around here and you'll find other good recommendations.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top