把数据库的查询结果重定向到文件
#!/bin/bash
function QueryDB()
{
filename=$(date +%Y_%-m_%d)_$(date +%H_%M_%S)
today=$(date +%Y_%-m_%d)
yesterday=$(date -d '-1 day' +%Y_%-m_%d)
mkdir /home/cet/temp/DataCacheUpdateTest/database/$today/
touch /home/cet/temp/DataCacheUpdateTest/database/$today/$filename.txt
echo $yesterday
psql "host=127.0.0.1 port=5432 user=postgres password=sa123456 dbname=PECSTAR_DATALOG_test" > /home/cet/temp/DataCacheUpdateTest/database/$today/$filename.txt <<-EOF
SELECT updatetime FROM pd_tb_40_01_${yesterday}_1;
EOF
one=1
linenum=$(expr $(cat /home/cet/temp/DataCacheUpdateTest/database/$today/$filename.txt | wc -l) - $one)
echo $linenum
if [ $linenum == 4 ]
then
echo "a 等于 b"
elif [ $linenum -gt 4 ]
then
echo "a 大于 b"
elif [ $a -lt $linenum ]
then
echo "Table is not exist"
else
echo "没有符合的条件"
fi
}
#
#SELECT * FROM public.pd_tb_40_01_2023_1_1_1
QueryDB