Shell command execution always giving zero as return value

Whenever the shell script is invoked by the scheduler the command execution return code is always captured as 0(Success).
If the same shell script is executed in command line via unix terminal, the command execution return code's are captured properly.

For example:
ls -ltr es_wrong_file ----> file not available in the filesystem
echo $? ----> return code $? will be 2(other than 0)

If the above example is executed via scheduler (which in turn invokes the script) - the return code is captured as 0 (success)
whereas if its a directly command line execution of the same script then the return code is captured properly as "2"

---------- Post updated at 06:43 AM ---------- Previous update was at 06:40 AM ----------

Oracle scheduler is used
Unix flavor is AIX6.1
shell used - ksh

It is coming as 2 only. I have tried few ways still i m getting 2 only. Better if you put your code..

Hi,

If the shell script is run in command line you will get it as 2...

but the same shell script when it is invoked by oracle scheduler - the return code is 0

I don't have oracle scheduler here on my system. But if that is not working you can use below code if you are just checking the presence of file.

if [[ -e file ]]
then
echo "pass"
else
echo "fail"
fi

It could simply mean that your script generates the file "es_wrong_file" at run-time and ls is able to list it at that particular time. Check if this file is being written to in your script. This could also be an intermediate file which your script could be cleaning up before termination.

So, don't leave people guessing and post the script if you can.

The script would have to exit with a non-zero exit code.

ls -ltr es_wrong_file ; REPLY=$?
exit ${REPLY}

It would indeed be better to test whether the file exists rather than letting the command fail. Use syntax appropriate to your O/S (many do not have "-e").