LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   shell script (https://www.linuxquestions.org/questions/linux-newbie-8/shell-script-181289/)

Greg_courageous 05-13-2004 11:37 PM

shell script
 
hello, 'I had to make a script in bash, cant i cant get it to work, as you can probably tell is pretty much just works off the system time and outputs a different greeting depending on the time...

if date+%H < 12 >= 24; then
echo Good Morning
else if date+%H <=12 >5
echo Good Afternoon
else if date+%H <=8 >24
echo Good Night
fi


What am i doing wrong??
Thanks for your help!

Greg

SciYro 05-14-2004 12:08 AM

did you remember to include the /bin/bash header in the top?

Greg_courageous 05-14-2004 12:22 AM

I think my problem has something to do with the else if statement, because if i just have

Code:

echo "The hour is " date+%H
it works

i dont think my syntax is correct...

homey 05-14-2004 01:01 AM

This kinda sounds like homework so here are some hints...

now=`date +%H`

use -le , -gt , and -a

use if , elif , else and fi

use echo " Something"

chii-chan 05-14-2004 01:25 AM

you must have that ` in the beginning and the end of the command. So the command should be:

echo "the hour is" `date +%H`.

no neeed to have that space inside the quotation for "the hour is".

How about:

#!/bin/bash

a=`date +%H`

if [ "$a" -lt 12 ] && [ "$a" -ge 24 ]
then
echo "Good Morning"
elif [ "$a" -le 12 ] && [ "$a" -gt 5 ]
then
echo "Good Afternoon"
else
echo "Good Night"
fi

you can replace that [ "$a" -lt 12 ] syntax with (("$a" < 12)).

take note the space in [] before the variable as compared to variable inside the (()).

Greg_courageous 05-14-2004 01:35 AM

Code:

if [ $wang -le 12 -a $wang -gt 24 ] then
      echo Good Morning
fi

if [ $wang -le 17 -a $wang -gt 12 ] then
      echo Good Afternoon
fi

if [ $wang -le 24 -a $wang -gt 17 ] then
      echo Good Night
fi

exit 0

still doesnt work
anyone have any ideas???

Greg_courageous 05-14-2004 01:41 AM

Code:

wang=`date +%H`


if [ $wang -le 12 -a $wang -gt 24 ]
then
      echo "Good Morning"
elif [ $wang -le 17 -a $wang -gt 12 ]
then
      echo "Good Afternoon"

else [ $wang -le 24 -a $wang -gt 17 ]

      echo "Good Night"
fi

works perfect =)
thanks guys

chii-chan 05-14-2004 01:42 AM

where are those quotation marks? that " " to include variable? you'll need that. ;)

SciYro 05-14-2004 02:50 AM

only if you want to break it

chii-chan 05-14-2004 03:01 AM

Quote:

Originally posted by SciYro
only if you want to break it
What do you mean? Is it going to break the script if it is like this:

if [ "$wang" -le 12 ]
then
echo "bla bla bla"
fi

?

It is the correct syntax in Advanced Bash Scripting Guide by Mendel Cooper. Please correct me if i'm wrong with this.


All times are GMT -5. The time now is 05:02 PM.