1

I got this error while running my script with nohup sh:

 syntax error near unexpected token `('

My script is as follow:

#!/bin/bash
report_log="report.log";
grep -A 3 'successful\|ERROR COUNT EXCEEDED' *.log  > ${report_log};
echo ${report_log};
MAX_ERR_COUNT_EXCEED_MSG="No Max Count Error.";
if grep "ERROR COUNT EXCEEDED" ${report_log}; then
   MAX_ERR_COUNT_EXCEED_MSG="MAX ERROR COUNT EXCEEDED, CHECK RECORD COUNT!";
fi
RESULT_MSG="Execution successful";
if grep '\([1-9]\d*\b\)' ${report_log} | grep 'data errors'; then
   RESULT_MSG="Execution with ERROR";
fi
cat ${report_log} <(echo "-----------") <(echo "${MAX_ERR_COUNT_EXCEED_MSG}") <(echo "${RESULT_MSG}") | mailx -s "Test Result" abc@test.com

It is a script to filter through some log files and generate a reporting email on the execution status from those files.

The error seems to come from the usage of <( in the script, but having added the bash shebang i thought it should work. The script run without issue if running using the command below:

. ./script.sh

Any help or advice is appreciated. Thanks.

  • What do you mean by "running my script with nohup sh"? Do you start it as nohup sh /path/to/your/script.sh or as nohup /path/to/your/script.sh? In the first case the shebang line is ignored because the interpreter is called explicitly. – Hauke Laging Jan 29 '15 at 9:09
  • Hi, i'm running it using nohup sh /path/to/script. In that case is there anyway i can overwrite the interpreter? This is because this script is called by another script and generally others run those scripts using nohup sh – ipohfly Jan 29 '15 at 9:13
  • when i try running it via nohup /path/to/script i get an error nohup: cannot run command 'script.sh': No such file or directory instead – ipohfly Jan 29 '15 at 9:17
  • So you cannot influence how the script is called. Can you modify the script? – Hauke Laging Jan 29 '15 at 9:17
  • have you tried nohup bash /path/to/myscript.sh ? – Archemar Jan 29 '15 at 9:19
1

The problem is that running the script as nohup sh /path/to/your/script.sh overrides the shebang line interpreter. When called as sh then bash turns off certain features (that is probably similar with other shells) and thus cannot parse process substitution any more.

The solution is to make sure that bash is running without restrictions. This can be done by checking for an environment variable and call the script again with bash (instead of sh):

#! /bin/bash

if [ "YES" = "$RUNNING_AS_BASH" ]; then
        eval 'cat <(echo "-----------")'
else
        test -f "$0" || exit 1
        RUNNING_AS_BASH="YES" exec bash "$0"
fi

Unfortunately bash as sh parses the whole if structure. Thus in order to avoid the error the incompatible syntax must be hidden in an eval statement.

This can be avoided if the eval action is taken out of the if structure because the rest of the script is not parsed:

#! /bin/bash

if [ "YES" != "$RUNNING_AS_BASH" ]; then
        test -f "$0" || exit 1
        RUNNING_AS_BASH="YES" exec bash "$0"
fi

cat <(echo "-----------")

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.