繁体   English   中英

将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 $1}' | awk -F "-" '{print $3}')  
export Day=$(echo `date +"%m-%d-%Y %H:%M:%S"` | awk -F " " '{print $1}' | awk -F "-" '{print $2}')
export Month=$(echo `date +"%m-%d-%Y %H:%M:%S"` | awk -F " " '{print $1}' | awk -F "-" '{print $1}')
export Hour=$(echo `date +"%m-%d-%Y %H:%M:%S"` | awk -F " " '{print $2}' | awk -F ":" '{print $1}')

  #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错误:列“ 2017”不存在第1行:... om log_archive,其中date_part('year',log_time)=“ 2017”和...

当我尝试不使用变量手动进行操作时,我会得到所需的结果。

请帮忙。

双引号( " )表示标识符。只需将其删除并使用裸数字,就可以了:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM