if [ $? -ne 0 ] then syntax error then unexpected if [ $? -ne 0 ] then syntax error then unexpected unix unix

if [ $? -ne 0 ] then syntax error then unexpected


This looks like bash rather than ksh

failed() {    echo "$0 failed at line number $1";    echo "moving $2 to failed folder"  }if [[ $? -ne 0 ]]then  failed $LINENO-2 $5 $6  fi


You need to be careful. The first operation on $? will usually clear it so that your if won't work anyway.

You would be better off using:

rc=$?echo $rcif [ $rc -ne 0 ]:

Other than that, it works fine for me:

$ grep 1 /dev/null$ if [ $? -ne 0 ]> then> echo xx> fixx$ grep 1 /dev/null$ echo $?;1$ if [ $? -ne 0 ]> then> echo yy> fi$ _

Note the lack of output in the last one. That's because the echo has sucked up the return value and overwritten it (since the echo was successful).

As an aside, you should let us know which UNIX and which ksh you're actually using. My working version is ksh93 under Ubuntu. Your mileage may vary if you're using a lesser version.


It looks like, from your update, your only problem now is the function call. That's most likely because you're defining it after using it. The script:

grep 1 /dev/nullrc=$?if [ $rc -ne 0 ]then        failed $rcfifailed(){        echo Return code was $1}

produces:

qq.ksh[6]: failed: not found

while:

failed(){        echo Return code was $1}grep 1 /dev/nullrc=$?if [ $rc -ne 0 ]then        failed $rcfi

produces

Return code was 1


you are missing semicolons at the end of the lines:

if [ $? -ne 0]; then   # …