Syntax error: unexpected 'else'


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Syntax error: unexpected 'else'
# 1  
Old 03-13-2020
Syntax error: unexpected 'else'

Im trying to pause my script for 23:59 to 6am but it is throwing error:
Code below:
Code:
#!/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

Moderator's Comments:
Mod Comment
Please wrap all code, files, input & output/errors in CODE tags.
It makes it far easier to read and preserves spaces for indenting or fixed-width data.

Last edited by rbatte1; 03-13-2020 at 09:54 AM..
# 2  
Old 03-13-2020
Now, where to start?


- With your syntax errors?

Beyond
Code:
bash: syntax error near unexpected token `else'

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

, and the sequence

Code:
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 sleeps the necessary amount of seconds until 6:00h in the morning if run between midnight and 6:00h. At all other times, it sleeps a minute and repeats the test. Feel free to adapt to your lower limit of one minute before midnight:

Code:
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.
This User Gave Thanks to RudiC For This Post:
# 3  
Old 03-13-2020
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.

Last edited by rbatte1; 03-13-2020 at 09:56 AM.. Reason: Grammatical corrections
# 4  
Old 03-13-2020
You'd probably better read into: man crontab for your timing issue.
# 5  
Old 03-13-2020
Hi sea,
Will crontab works can you write one example where it will run only in between 6am to 00 daily
# 6  
Old 03-13-2020
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 Smilie

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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Syntax error: Unexpected '('

I've been trying to figure this issue for almost 2 hours now... I dont see/find any typos or other code mismatches.. but maybe i'm looking at the wrong places.... Here's the console output: $ LC_ALL=C $ cd prjs/SWARM/ ; time source ./rc bash: cd: prjs/SWARM/: No such file or directory... (3 Replies)
Discussion started by: sea
3 Replies

2. Shell Programming and Scripting

Syntax error: `]' unexpected

I am getting this error Syntax error: `]' unexpected. Did I do something wrong with elif? Does ksh not like double brackets? if ]; then #echo hi source ~/.bashrc; elif ]; then #echo hi source ~/.kshrc; fi (5 Replies)
Discussion started by: cokedude
5 Replies

3. UNIX for Beginners Questions & Answers

Syntax error near unexpected token

Dears, While executing the below script im getting the error at line 30. Please let me know what changes to be done to fix this. test.sh: line 30: syntax error near unexpected token `done' test.sh: line 30: ` done ' #!/bin/sh # Rev. PA1 # author: eillops # date: 26-04-2018 # #... (1 Reply)
Discussion started by: Kamesh G
1 Replies

4. Shell Programming and Scripting

Syntax error `(' unexpected

I have written this in my script but while running i am getting syntax error `(' unexpected. unload to "$BACKUP_DIR/n_fac_fid-$clliname" select * from n_fac_fid where fac_accesskey in (select fac_accesskey From n_fac_ap_fid where ap_clli="$clliname"); Any help appreciated. (3 Replies)
Discussion started by: suryanmi
3 Replies

5. Shell Programming and Scripting

Syntax error: 'fi' unexpected

unzip file.zip if ] ; then echo "Success" else echo "Some failure." fi ; I tried many time to detect the unzip error, but it keep show the syntax error wherever how I change the syntac. Hope someone can help me fix the issue, thanks. Please use code tags next time for your code and... (5 Replies)
Discussion started by: duncanyy
5 Replies

6. Shell Programming and Scripting

Help with ksh syntax error Unexpected Fi

Issue resolved, thanks (6 Replies)
Discussion started by: dangell82
6 Replies

7. Shell Programming and Scripting

ksh syntax error: `(' unexpected

So I am trying to convert my bash script into ksh, and this is what I have in the file so far: #!/bin/ksh login() { if then sendcmd BETA else sendcmd "$(xxd -c 32 -g 0 ${ZETA_ZETA} | awk '{print $2}')" fi } But when I run it: $ ./test.sh ... (1 Reply)
Discussion started by: guitarscn
1 Replies

8. Shell Programming and Scripting

syntax error: `$' unexpected

Hi all, Am very new to Unix and am currently Involved in Migrating some Shell Scripts from AIX 4 to Solaris 10. While using teh for loop am getting the below error: $ echo $SHELL /usr/bin/ksh $ for file in $(ls *SEBE*) syntax error: `$' unexpected while the same works without issue on... (4 Replies)
Discussion started by: paragkhanore
4 Replies

9. Shell Programming and Scripting

unexpected syntax error

Hi, i am getting following syntax error ...kingly advice why is it coming ?? #!/bin/bash find . -name "common.log" if ; then echo "1" fi Himnashu@home /bin $ ./a.sh ./a.sh: line 7: syntax error near unexpected token `fi' ./a.sh: line 7: `fi' (9 Replies)
Discussion started by: himvat
9 Replies

10. Shell Programming and Scripting

sh: syntax error: `...' unexpected???

Hello all, I want to create a script that polls every hour a directory for the existence of a file. The file I look for is a `token` dropped by an external process at the completion of a successful FTP process. I wrote this script `checkfile.ksh`: #!/usr/bin/ksh if ] then mailx... (5 Replies)
Discussion started by: alan
5 Replies
Login or Register to Ask a Question