Shell script has to run until the status value is updated

Hi All,

Need some help like how to exit from the script after updating the column in data base.

db2 connect to DB
STATUS=$(db2 "SELECT STATUS FROM XYZ )
echo $STATUS

Initially the status value will be '4' or 'NOT YET RUN'.

The Shell script has to run until the status value is updated with '0' in data base

Example forever loop and if status is 0, then break the loop. Case is nice command to use string comparing.

#!/bin/bash
# or ksh
while true
do
      # your db code here
      # after that:
      case "$STATUS" in
            0) break ;;   # break loop
            4) echo "status 4" ;;
            NOT*YET*RUN) echo "status not yet" ;;
            #....
             *)  echo "other status $STATUS" ;;
       esac
       sleep 10  # sleep 10 sec and poll again
done
echo "Done"

1 Like

You might want to run db2 -x SELECT ... to get rid of the heading and 1 record(s) selected.

1 Like

Thanks hergp. I tried this code but facing some issue.

if i hard code like status=0 or with some other values (4,Not Yet Run) the script is working fine .But if i am pulling the status value from db, every time it is moving in to other status for all the values 0,4, and Not yet run.

Kindly suggest.

I am not sure, if I understand you correctly. Do you mean the SQL SELECT statement or the case-construct afterwards? Can you give an example?

1 Like

Some thing like this.

If I use Status=0 in the script no issue. It is immediately coming out of the loop because of break condition in the case statement

But if I fetch Status value from DB like below.
Status= db2 " Select Status from X table" then it is returning "Other Status 0" from the case statement which is wrong. It has to immediately come out of the loop. But its not happening

---------- Post updated at 06:47 AM ---------- Previous update was at 06:34 AM ----------

Currently the status value is '0' . But if I ran the below script it is returning "Other Status 0"

while true
do
db2 connect 
STATUS=$(db2 -x "SELECT JOB_STATUS FROM X WHERE NAME='Test1'")
# after that:
case "$STATUS" in
'0') break ;; # break loop
'4') echo "status 4" ;;
'NOT*YET*RUN') echo "Job Running" ;;
#....
*) echo "other status $STATUS" ;;
esac
sleep 10 # sleep 10 sec and poll again
done
echo "Done"

Looks like the output of the select statement is not just 0, but something like 0 .

If the status code in the database does not contain blanks, you just could remove the quotes from the case statement

case $STATUS in

which would effectively strip leading and trailing blanks from the status code.

Otherwise change the patterns within the case statement to include optional blanks.

case "$STATUS" in
       *( )0*( )) break ;;
       ...
1 Like

Excellent hergp. Script is working exactly what i am expected.
Many Thanks for your help.:slight_smile: