Take the 2-minute tour ×
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

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.

share|improve this question
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 Nov 20 '13 at 9:52
    
it is the set -e problem. and it's more a ksh issue. –  zinking Nov 22 '13 at 6:12
add comment

2 Answers

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

share|improve this answer
    
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 Nov 21 '13 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. –  Peter Schuetze Nov 21 '13 at 16:32
add comment

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

set -e
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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