Syntax error: unexpected 'else'

Im trying to pause my script for 23:59 to 6am but it is throwing error:
Code below:

#!/system/bin/sh

while true
do
TIME=$(date '+%H%M%S')
if [ $TIME -ge 235900 ] -a [ $TIME -le 060000 ]
then*
echo "time between  noon and morning"*
echo sleep time $TIME
sleep 3
else
then
echo "time all others"
fi
done

Now, where to start?

  • With your syntax errors?

Beyond

bash: syntax error near unexpected token `else'

, there are

'[' 100652 -ge 235900 ']' -a '[' 100652 -le 060000 ']'
bash: [: too many arguments
'then*'
then*: command not found

, and the sequence

else
then

also doesn't make any sense.

  • By commenting that using decent code structuring like indenting and block building helps you and others read, understand and maintain your code?

  • With the question what you want to achieve with that small code snippet? It doesn't seem to make sense to have an infinite loop running all day long, wasting enormous amounts of resources for doing nothing? There are dedicated tools for dealing with time dependent tasks, like cron or at .

  • With the remark that date / time arithmetics is extremely complex by itself and becomes more difficult if crossing midnight?

Here is a sample code doing what I guess you want but starting at midnight. It sleep s the necessary amount of seconds until 6:00h in the morning if run between midnight and 6:00h. At all other times, it sleep s a minute and repeats the test. Feel free to adapt to your lower limit of one minute before midnight:

while true
  do    TIME=$(date '+%H%M%S')
        if [ $TIME -ge 000000 -a  $TIME -le 060000 ]
          then  echo "time between noon and morning"*
                echo sleep $(( $(printf "06:00\nnow\n" | date -f- +%s-) 0 ))
                sleep $(( $(printf "06:00\nnow\n" | date -f- +%s-) 0 ))
          else  echo "time all others"
                sleep 60
        fi
   done

I'm sure there will be better solutions if you explain exactly what you're after in a larger perspective.

1 Like

Hi RudiC,
Thanks for the reply. I have a script named termination.sh where I want this to run keep running only in the time specified time i.,e 06 to 23:59 but I don't want this to run/execute again and again,script once run will do the job for me.The only thing I want is to make termination.sh script to sleep for 00:00 to 06:00 am. termination.sh script is a java script where I have java code inside the jar.

--- Post updated at 12:02 PM ---

I want my termination script to run only between morning from 06:00am to 00:00.

--- Post updated at 12:10 PM ---

I want to run termination.sh script inside the above given script where it should be running only in between 06 to 23:59.

You'd probably better read into: man crontab for your timing issue.

Hi sea,
Will crontab works can you write one example where it will run only in between 6am to 00 daily

I'm not using CRON, I have no need for it.
Please give it a try in your other thread, we'll help you from there on.

I had used it like 5 years ago - ONCE, while I was writing a script to handle cron :wink:

From what I recall, it's not THAT hard, once you get past the time code lines....
But basicly it's simple:

<command> <time code settings>