0

I want to write a shell script that checks if trigger file is available in my directory or not. If not available I want the code to wait for, say 1 hour, and check again for availability until found. Once found, the script should complete successfully.

The trigger file name has a constant component and then a variable datetime component. E.g. Transfer-20210813061635681177 wherein Transfer- is constant and rest varies everyday.

Below is what I have tried, I put it in a Test.ksh file and tried to execute by giving ksh Test.ksh on unix server command line:

#!/bin/ksh  
if [ ! -f /data/source/Transfer-* ] ; then  
   sleep 3600  # wait 60 minutes  
fi  
[ -f /data/source/Transfer-* ]  && echo 'trigger found' || echo 'trigger not found'  
exit 0

It gives the following error:

Test.ksh :Line 6: syntax error at line 6: 'if' unmatched

What could I be doing wrong here? Or how can this be further improvised and corrected. Request help.

4
  • Perhaps you file has some trailing non-printing characters, such as DOS-style (CRLF) line endings? Aug 13, 2021 at 16:50
  • @steeldriver, i did check for that using Notepad++ and verified there is nothing after exit 0. No CR LF
    – BasicQs
    Aug 13, 2021 at 17:14
  • If you are using notepad++, then please go to Edit -> EOL Conversion and make sure to select "Unix (LF)" Aug 13, 2021 at 18:08
  • @steeldriver, looks like your suggestion worked, thank you so much!
    – BasicQs
    Aug 13, 2021 at 19:25

1 Answer 1

0

Example showing how running the script works fine, thus reinforcing the ideas from @steeldriver + @UnixBeginner that there's some formatting problem in play. Once change made was cranking your 3600 seconds down to 3 for test purposes.

$ cat x2
#!/bin/ksh
if [ ! -f /data/source/Transfer-* ] ; then
   sleep 3  # wait 60 minutes
fi
[ -f /data/source/Transfer-* ]  && echo 'trigger found' || echo 'trigger not found'
exit 0
$ ./x2
trigger not found
$
1
  • it is still erroring out with the same message, this time on line 10 though. Also says $ not found[No such file or directory]
    – BasicQs
    Aug 13, 2021 at 17:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .