LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-14-2006, 05:29 PM   #1
Joe Mongi
LQ Newbie
 
Registered: May 2006
Posts: 6

Rep: Reputation: 0
Smile 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.
 
Old 05-14-2006, 05:34 PM   #2
masonm
Senior Member
 
Registered: Mar 2003
Location: Following the white rabbit
Distribution: Slackware64 -current
Posts: 2,300

Rep: Reputation: 90
try the full path "/bin/date"
 
Old 05-14-2006, 06:20 PM   #3
Joe Mongi
LQ Newbie
 
Registered: May 2006
Posts: 6

Original Poster
Rep: Reputation: 0
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)]'}'

 
Old 05-14-2006, 06:39 PM   #4
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,749

Rep: Reputation: 5928Reputation: 5928Reputation: 5928Reputation: 5928Reputation: 5928Reputation: 5928Reputation: 5928Reputation: 5928Reputation: 5928Reputation: 5928Reputation: 5928
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.
 
Old 05-14-2006, 07:27 PM   #5
Joe Mongi
LQ Newbie
 
Registered: May 2006
Posts: 6

Original Poster
Rep: Reputation: 0
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)]'}'
 
Old 05-14-2006, 08:12 PM   #6
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,749

Rep: Reputation: 5928Reputation: 5928Reputation: 5928Reputation: 5928Reputation: 5928Reputation: 5928Reputation: 5928Reputation: 5928Reputation: 5928Reputation: 5928Reputation: 5928
Lets try again
hour=$(date | awk {'print $4'})
 
Old 05-14-2006, 08:43 PM   #7
rkelsen
Senior Member
 
Registered: Sep 2004
Distribution: slackware
Posts: 4,462
Blog Entries: 7

Rep: Reputation: 2561Reputation: 2561Reputation: 2561Reputation: 2561Reputation: 2561Reputation: 2561Reputation: 2561Reputation: 2561Reputation: 2561Reputation: 2561Reputation: 2561
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
 
Old 05-15-2006, 04:37 AM   #8
Joe Mongi
LQ Newbie
 
Registered: May 2006
Posts: 6

Original Poster
Rep: Reputation: 0
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
 
Old 05-15-2006, 05:03 AM   #9
timmeke
Senior Member
 
Registered: Nov 2005
Location: Belgium
Distribution: Red Hat, Fedora
Posts: 1,515

Rep: Reputation: 61
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.
 
Old 05-15-2006, 09:26 AM   #10
Lotharster
Member
 
Registered: Nov 2005
Posts: 144

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


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
Newbie Alert!!! B00H Linux - Software 3 10-27-2005 03:47 PM
Newbie Alert! (Again) robertwolfe LinuxQuestions.org Member Intro 1 04-29-2005 02:41 PM
Alert for logins sachinh Linux - General 1 08-30-2004 07:05 AM
Security alert for 5/6/02 PostDeals Linux - Security 2 05-07-2002 08:11 AM
ALERT!!! ALERT!!! I messed up the UNIX!!! Firew Linux - Software 1 11-05-2001 11:48 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:39 AM.

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