0

I am very new to shell scripting. The script should fire a command if the given time as command line argument is equal to the system time, else it should wait(poll) for the given time.

I started with very basics, but i am stuck here only:-(

#!/bin/sh

now=$(date +%k%M)
cur="055" # should be given as command line arg
if ($now == $cur)
then
    echo "Fire command here"
else
    echo "poll for time"
fi

When i execute the script:

./script.sh: line 5: 055: command not found

poll for time

Thanks for the help.

james
  • 13
  • 1
  • 5

5 Answers5

3

I think the above is just a small syntax error, instead of:

if ($now == $cur)

You may want to do this:

if [ $now -eq $cur ]  #very basic comparison here, you may want to have a
                      #look at the bash comparisons

Update Could you change the variable to,

$cur=$(date +%H%M)

And in case the input is not provided by you, you should remove the space in front of the $now

now=$(echo $now | sed 's/\s//g') #this removes the spaces in the input
Juto
  • 1,246
  • 1
  • 13
  • 24
  • if i use `if [ $now -eq $cur ]` , i get `./script.sh: line 5: [: missing `]'` `poll for time` , i don't know why? – james Sep 02 '13 at 14:10
  • Did you put a space between the `[` and the `$now`? – Juto Sep 02 '13 at 14:12
  • I used the syntax suggested by you, it reflects the error:`./script.sh: line 5: [: missing `]'` – james Sep 02 '13 at 14:14
  • I mean, are you going to get from the command line the values padded by `\s` if the hour value is less than 10? – Juto Sep 02 '13 at 14:17
0

You can run a program @ a particular time with :

crontab -e

and

0 14 * * * command

to run command @ 14 PM (by example)

Gilles Quénot
  • 173,512
  • 41
  • 224
  • 223
0
begin="`date '+%s'`"
final=... #should be converted to seconds since epoch...
sleep $((final - begin))
exec_your_command
MasterID
  • 1,510
  • 1
  • 11
  • 15
0

The problem you described seems to be exactly what crontab is designed to handle, from wikipedia "Cron is driven by a crontab (cron table) file, a configuration file that specifies shell commands to run periodically on a given schedule."

Here is a quick reference, it is reletively barebones, but should be enough to determine if it meets your needs.

0

Use following code

if [ $now == $cur ]