【发布时间】:2021-02-23 19:43:43
【问题描述】:
我想在 bash 脚本中获取某个表中记录的计数,但返回时 $num 为空(应该是数字)。当我直接在pgadmin中运行时查询是正确的,我可以获得行数。有谁知道怎么回事?
declare -a ROW=($(psql \
-X \
-U $DB_USER \
-h $DB_HOST \
-d $DB_NAME \
-p $DB_PORT \
--set ON_ERROR_STOP=on \
--no-align \
-t \
--field-separator ' ' \
--quiet \
-c "SELECT count(*) as num
FROM table_test)")
)
echo "num_error: $num here"
if [[ $num == 0 ]]; then
echo "no error occur within the past 1 hour"
elif [[ $num == '' ]]; then
echo "return nothing"
else echo "$num"
fi
【问题讨论】:
-
您将输出分配给
ROW,然后您检查$num... -
num是 SQL 变量,而不是 shell 变量。