1

I have a simple ksh script that executes db2 queries, such as

#!/bin/ksh
#some initialization
#step1,2,3
set -e
db2 -x "select * from table a "
#step4

The main issue seems to be caused by the db2 command. If the query returns records the shell script works perfectly fine. But if the query returns no results, then step4 will not be executed at all, and exits the shell immediately. This doesn't make sense. If the query didn't return anything, the shell still shouldn't crash anyway.

Can someone explain the reason ?

it turns out that there is a option "set -e" in the long script which will capture all unhandled execptional scenarios and abort the shell execution.

2
  • 2
    Can you give us a complete example? with the provided code, we cannot guess what kind of step4 you are doing. The -x does not return the DB2 headers.
    – AngocA
    Commented Nov 20, 2013 at 9:52
  • it is the set -e problem. and it's more a ksh issue.
    – zinking
    Commented Nov 22, 2013 at 6:12

2 Answers 2

1

I guess it has something to do with the fact, that db2 will return an error code of 1 when no records were found.

2
  • but this contradict to the fact that when we run this manually in a shell, although no results returned the currrent shell won't be destroyed.
    – zinking
    Commented Nov 21, 2013 at 1:18
  • You should probably give us more context. How about the next 5 lines after the db2 command. Without that info we can't help you much. I suspect that you will have an exit 1 following the db2 command. Depending on how you called your script, it will destroy your shell. Run your script with ksh my_cool_script.sh and it will not crash your login shell. Commented Nov 21, 2013 at 16:32
1

the issue lies in an overlooked option setting at the start of the script.

set -e

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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