包含字符串的Bash变量在读取后清空

时间:2016-05-17 10:46:38

标签: mysql bash shell

我在bash中运行mysql查询以获取行计数,然后使用while read检查值。

如果它为0,我设置一个状态消息变量来反映它,或者我将状态设置为下一步并继续。

当我echo消息时看起来设置正常,但是一旦循环结束,变量就为空,我的查询以更新状态的DB失败:

# remove header row from mysql results then loop over remaining line
sed 1d countRows.dat | while read cnt
do
    if [ "$cnt" == "0" ] 
    then
        echo no results
        TABLE_STATUS_MESSAGE="no results in temp, generation stopped"
    else
        echo "set temp table as active ($cnt rows)"
        TABLE_STATUS_MESSAGE="setting temp table as active"
        echo "message after set: $TABLE_STATUS_MESSAGE"
        # make sql from template
        sed -e s/@TABLE_NAME@/$TABLE_NAME/g < swapTables.template > swapTables.sql
        mysql -h "$MYSQL_HOST" -u "$MYSQL_USERNAME" --password="$MYSQL_PASSWORD" dbname < swapTables.sql
        echo "message after swapTables sed: $TABLE_STATUS_MESSAGE"
    fi
    echo "message after 0 check: $TABLE_STATUS_MESSAGE"
done

echo "message before use: $TABLE_STATUS_MESSAGE"

echo 'UPDATE `rates_table` SET `status` ="'$TABLE_STATUS_MESSAGE'" WHERE `id` = '$TABLE_ID

以上代码产生以下输出:

set temp table as active (70452 rows)
message after set: setting temp table as active
message after swapTables sed: setting temp table as active
message after 0 check: setting temp table as active
message before use:
UPDATE `rates_table` SET `status` ="" WHERE `id` = 2

我看不出我在message after 0 checkmessage before use之间正在做什么来清除$TABLE_STATUS_MESSAGE

我希望这只是一个错字或者是愚蠢的东西,有人发现什么吗?

0 个答案:

没有答案