将 Unix 变量传递给 psql date_part 函数
Passing Unix Variable to psql date_part function
这是我的脚本:
export Year=$(echo `date +"%m-%d-%Y %H:%M:%S"` | awk -F " " '{print }' | awk -F "-" '{print }')
export Day=$(echo `date +"%m-%d-%Y %H:%M:%S"` | awk -F " " '{print }' | awk -F "-" '{print }')
export Month=$(echo `date +"%m-%d-%Y %H:%M:%S"` | awk -F " " '{print }' | awk -F "-" '{print }')
export Hour=$(echo `date +"%m-%d-%Y %H:%M:%S"` | awk -F " " '{print }' | awk -F ":" '{print }')
#Output of exports
#Year 2017
#Day 04
#Month 10
#Hour 05
psql -U postgres -d dbname -t -c "select id, time, code, level, message, trace from logarchive where date_part('year',log_time) = \"$Year\" and date_part('month',log_time) = \"$Month\" and date_part('day',log_time) = \"$Day\" and date_part('hour',log_time) = \"$Hour\" and log_level = 'ERROR' limit 5;" > /tmp/List.txt
我得到的错误:
[root]# sh new.sh ERROR: column "2017" does not exist LINE 1: ...om
log_archive where date_part('year',log_time) = "2017" and...
当我尝试在没有变量的情况下手动执行时,我得到了想要的结果。
请帮忙。
双引号 ("
) 表示标识符。只需删除它们并使用裸数字,你应该没问题:
psql -U postgres -d dbname -t -c "select id, time, code, level, message, trace from logarchive where date_part('year',log_time) = $Year and date_part('month',log_time) = $Month and date_part('day',log_time) = $Day and date_part('hour',log_time) = $Hour and log_level = 'ERROR' limit 5;" > /tmp/List.txt
这是我的脚本:
export Year=$(echo `date +"%m-%d-%Y %H:%M:%S"` | awk -F " " '{print }' | awk -F "-" '{print }')
export Day=$(echo `date +"%m-%d-%Y %H:%M:%S"` | awk -F " " '{print }' | awk -F "-" '{print }')
export Month=$(echo `date +"%m-%d-%Y %H:%M:%S"` | awk -F " " '{print }' | awk -F "-" '{print }')
export Hour=$(echo `date +"%m-%d-%Y %H:%M:%S"` | awk -F " " '{print }' | awk -F ":" '{print }')
#Output of exports
#Year 2017
#Day 04
#Month 10
#Hour 05
psql -U postgres -d dbname -t -c "select id, time, code, level, message, trace from logarchive where date_part('year',log_time) = \"$Year\" and date_part('month',log_time) = \"$Month\" and date_part('day',log_time) = \"$Day\" and date_part('hour',log_time) = \"$Hour\" and log_level = 'ERROR' limit 5;" > /tmp/List.txt
我得到的错误:
[root]# sh new.sh ERROR: column "2017" does not exist LINE 1: ...om log_archive where date_part('year',log_time) = "2017" and...
当我尝试在没有变量的情况下手动执行时,我得到了想要的结果。
请帮忙。
双引号 ("
) 表示标识符。只需删除它们并使用裸数字,你应该没问题:
psql -U postgres -d dbname -t -c "select id, time, code, level, message, trace from logarchive where date_part('year',log_time) = $Year and date_part('month',log_time) = $Month and date_part('day',log_time) = $Day and date_part('hour',log_time) = $Hour and log_level = 'ERROR' limit 5;" > /tmp/List.txt