1

I've made a script which collects info. One of the things it does is checking if a file or directory exists. If yes, it copies it to the /tmp directory.

The script fails with

0403-057 Syntax error at line 3 : `then' is not matched.

This is the statement that fails:

if [ -d /etc/nginx ];
then
cp -R /etc/nginx/* /tmp/
fi
  • 1
    That looks fine to me. Check that there aren't any invisible characters like non-breaking spaces or carriage returns at line ends. od -c script.sh or such should tell. – ilkkachu Mar 11 at 9:42
1

The script is fine. The error you describe most likely means you have Windows-style line endings. I can reproduce it by adding \r to the end of each line:

$ cat script.sh
if [ -d /etc/nginx ];
then
cp -R /etc/nginx/* /tmp/
fi
$ sed 's/$/\r/' script.sh 
$ ksh script.sh 
script.sh: syntax error at line 5: `if' unmatched

You probably edited the file on a Windows computer and that would have inserted \r\n line endings instead of the normal *nix \n line endings. Just remove them and you should be fine:

sed -i 's/\r//' script.sh  

That might not work on AIX's sed though. If it doesn't, use this instead:

sed 's/\r//' script.sh  > tempFile && mv tempFile script.sh

Or

tr -d '\r' < script.sh > tempFile && mv tempFile script.sh

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.