Null Handling in Until loop. . .loop won't stop

Hi Im running this script, which is supposed to find the max value build some tables and then stop running once all the tables are built. Thing is , it keeps assigning a null value to $h and then $g is null so it keep building tables i.e. testupdateNUL. How can I stop this? Here is what I have:

j=`/usr/local/mysql/bin/mysql --skip-column-names $US $PW $DB -e "select count(*) from loyalmarkets" ;`
echo $j
limit=$j
var=0
until ((limit=var)); do
h=`/usr/local/mysql/bin/mysql --skip-column-names $US $PW $DB -e "select max(orgunit1) from loyalmarkets where orgunit1 !=' ';"`
echo $h
g=`echo $h | cut -c1-3`
echo loyaltest$g
end=`date +%F`
limit=$j
var=0
until ((limit=var)); do
h=`/usr/local/mysql/bin/mysql --skip-column-names $US $PW $DB -e "select max(orgunit1) from loyalmarkets where orgunit1 !=' ';"`
echo $h
g=`echo $h | cut -c1-3`
echo loyaltest$g
end=`date +%F`
echo $end
/usr/local/mysql/bin/mysql --skip-column-names $US $PW $DB -e "drop table if exists testupdate$g; create table testupdate$g type=myisam as select store_name, account_id, local_request_date, order_id, svat
ran_type_id, amount, promo_program_id from request_detail rd force index(local_request_date)join store s on rd.store_id = s.store_id and orgunit1 = '$h' where rd.local_request_date >= '$end' - interval 1 day andrd.local_request_date < '$end' and currency_type_id = 2 and svatran_type_id in (2,4,6) limit 10; delete from loyalmarkets where orgunit1 = '$h';"
done

Try

((limit == var))

As it was written above it means: assign the value of var to limit. The value of limit determines the outcome, if more than 0 then the outcome is true.

Thanks for looking. Unfortunately that did not work. . . .

Well it was just a start. There are more issues:

  • limit and/or var never get changed inside the loop so the loop never ends
  • there is only one done statement
  • there are unnecessary repeats of the same code

This code needs a bit of fixing. You might want to try indenting your code for clarity.

I see var set to zero before the internal until, I see limit set to $j, but nothing ever changes it -

You have two until's and one done. The code as shown can't be running - it would die with "syntax error: unexpected end of file"