LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Automatic Alert (https://www.linuxquestions.org/questions/linux-newbie-8/automatic-alert-444791/)

Joe Mongi 05-14-2006 05:29 PM

Automatic Alert
 
I am new to Linux, I am learning shell scripts.
I wrote a script which will alert/ give message from Monday to Friday(5 weekdays) at 8:15 that "Welcome to Work Place"

#!bin/bash
HOUR='date | grep {'echo $4'}'
MINUTE='date |grep {'grep $5'}'
DAY='date | grep ('cal[Mon - Fri]')'
while true; do
if [$HOUR -eq 8] -a [$MINUTE -eq 15]; then
echo $DAY "Welcome to work place"
else
sleep 13500s
fi
done

I got the error message that /date command not found on line 2, 3 and line 4.
I need your assistance and be blessed.

masonm 05-14-2006 05:34 PM

try the full path "/bin/date"

Joe Mongi 05-14-2006 06:20 PM

Automatic alert
 
Quote:

Originally Posted by masonm
try the full path "/bin/date"

I changed the script by adding the /bin/date path, still I got the error message.
This is the script:

#!/bin/bash
/bin/date
hour='date |grep {'echo $4'}'
min='date |grep {'echo $5'}'
day='date |grep {'[cal (Mon - Fri)]'}

while true;
do
if [$HOUR -eq 21] = [$MIN -eq 45]; then
echo $DAY "Good Morning"
else
sleep 13500s
fi
done


The error message
day: line 3: }: command not found
day: line 4: }: command not found
day: line 5: syntax error near unexpected token `('
day: line 5: `day='date |grep {'[cal (Mon - Fri)]'}'

:newbie:

michaelk 05-14-2006 06:39 PM

Hint:
time='date | awk {'print $4}'

Will assign time to the 4th variable and looking at the ouput of the date command would be time.

You can use string commands to seperate out hours and minutes.

Joe Mongi 05-14-2006 07:27 PM

Quote:

Originally Posted by michaelk
Hint:
time='date | awk {'print $4}'

Will assign time to the 4th variable and looking at the ouput of the date command would be time.

You can use string commands to seperate out hours and minutes.

I have changed the script, but still I got the same error message.

#!/bin/bash
/bin/date

hour='date | awk {'print $4'}'
min='date | awk {'print $5'}'
day='date |awk {'print [cal (Mon - Fri)]'}'

while true;
do
if [$HOUR -eq 8] -a [$MIN -eq 15]; then
print -v $DAY "Good Morning"
else
sleep 13500s
fi
done

Error message:
day: line 3: }: command not found
day: line 4: }: command not found
day: line 5: syntax error near unexpected token `('
day: line 5: `day='date |awk {'print [cal (Mon - Fri)]'}'

michaelk 05-14-2006 08:12 PM

Lets try again
hour=$(date | awk {'print $4'})

rkelsen 05-14-2006 08:43 PM

How about this:

Code:

#!/bin/bash
HOUR=$(/bin/date | awk {'print $4'})
MIN=$(/bin/date | awk {'print $5'})
DAY=$(/bin/date |awk {'print [cal (Mon - Fri)]'})

while true;
do
    if [$HOUR -eq 8] -a [$MIN -eq 15]; then
        print -v  $DAY "Good Morning"
    else
        sleep 13500s
    fi
done


Joe Mongi 05-15-2006 04:37 AM

Automatic alert
 
Quote:

Originally Posted by rkelsen
How about this:

Code:

#!/bin/bash
HOUR=$(/bin/date | awk {'print $4'})
MIN=$(/bin/date | awk {'print $5'})
DAY=$(/bin/date |awk {'print [cal (Mon - Fri)]'})

while true;
do
    if [$HOUR -eq 8] -a [$MIN -eq 15]; then
        print -v  $DAY "Good Morning"
    else
        sleep 13500s
    fi
done


The first two variables now are working fine, the problems still in the third variable $DAY. I got this error message:

awk: {print [cal (Mon - Fri)]}
awk: ^ syntax error
awk: {print [cal (Mon - Fri)]}
awk: ^ syntax error
day: line 10: [10:16:27: command not found

timmeke 05-15-2006 05:03 AM

Instead of using "awk" or "grep" to get what you want, it's a lot easier just to use
date's own options for date formatting.
ie:
Code:

HOUR=`/bin/date +%H`
MIN=`/bin/date +%M`
DAY=`/bin/date +%A`
...

See also "man date" for more date/time formatting options.

A few other remarks:
-I doubt that there is something like a "print" command in Bash. Use "echo" to print messages on a terminal
instead. Check out "man bash" for this one.
-Your loop isn't ending. You should add an "exit" after echo'ing your "Good morning" message.

Lotharster 05-15-2006 09:26 AM

The main problem with your script is that you are using an apostrophe instead of a `.

When you need command substitution, you have to use
Code:

`command`
or
$(command)

The latter one has the advantage that you can nest several command substitutions.
E.g. the line
Code:

DAY=`date | grep (`cal[Mon - Fri]`)`
will not work, because bash will try to interprete `date | grep (` and `)` as commands. So you have to use
Code:

DAY=$(date | grep ( $(cal[Mon - Fri]) ) )
BTW, I'm not sure what you are trying to accomplish with cal[Mon - Fri]. Did you read the man page of cal?

Lotharster


All times are GMT -5. The time now is 11:05 AM.