I have a simple database query I wrapped in my bash script.
In case my query retrieves one row, I want to do several things. If my query retrieves zero rows, I want to terminate my script gracefully.
What is the best way to check if my script retrieves zero rows? Can you please provide some pointers?
Thank you.
Here’s a working bash script to get number of rows from the DB. Just pass the variables and replace FROM part of the sql with yours.
HINT:
Replace the last line echo $NUMROWS with exit $NUMROWS and your script will exit gracefully if there are no records. Wrap it in a bash if statement if you want to do other things, as below.