将查询结果存储在unix中的kshell变量中

时间:2013-07-04 04:50:05

标签: unix ksh

我想将查询结果存储到k-shell脚本中的变量中。到目前为止,我已经做了:

WeekEndingDate=`nzsql << endl | grep KEEP | sed 's/KEEP//;s/[ ]//g' 

select 'KEEP', max(Week1) from Table_Week; 

endl` 

echo Week ending date: $WeekEndingDate

MonthEndingDate=`nzsql << endl | grep KEEP | sed 's/KEEP//;s/[ ]//g' 

select 'KEEP', max(Month1) from Table_Week; 

endl`

echo Month ending date: $MonthEndingDate

我运行时的结果是:

Week ending date: |2013060801 

Month ending date: |2013060804

有一个加上前缀的管道。我需要没有管道的结果:

Week ending date: 2013060801 

Month ending date: 2013060804

请帮助我。

感谢。

1 个答案:

答案 0 :(得分:0)

试试这个:

WeekEndingDate=`nzsql -A -t -c "select max(Week1) from Table_Week"`
echo Week ending date: ${WeekEndingDate}

MonthEndingDate=`nzsql -A -t -c "select max(Month1) from Table_Week"`
echo Month ending date: ${MonthEndingDate}

您正在添加&#39; KEEP&#39;在sql查询中,然后从输出中删除它,因此首先添加它是没有意义的。您可以尝试以下方法,看看是否有帮助:

WeekEndingDate=`nzsql << endl
set pagesize 0 feedback off verify off heading off echo off
select trim(max(Week1)) from Table_Week; 
endl` 

echo Week ending date: ${WeekEndingDate}

MonthEndingDate=`nzsql << endl
set pagesize 0 feedback off verify off heading off echo off
select trim(max(Month1)) from Table_Week;
endl`

echo Month ending date: ${MonthEndingDate}