2013-06-28 100 views
0
while IFS=# read -r process_id source destination type 
do 
     echo "Process id: $process_id" 
     echo "Source: $source" 
     echo "Destination: $destination" 
     case "$type" in 
       2) 
       echo "Type is outbound: $type" 
         contact=$(sqlplus -s ${SQLPLUS_INFO} <<EOF 
         SET PAGESIZE 0 
         SELECT email FROM table WHERE partner = '${destination}'; 
         exit 
         EOF 
         ) 
       echo 
       echo ${contact} 
       echo 
       ;; 

大家好,如何将变量传递给shell脚本中的查询?

总部设在上面的代码中,如何从$目标中的值传递给查询?上面的例子不工作,即使是这些其他的:

SELECT email FROM table WHERE partner = '"${destination}"'; 
SELECT email FROM table WHERE partner = '$destination'; 

任何帮助将是有益的!谢谢!

+1

取出'contact = $('和')'来查看是否是问题的根源。我希望这能够按原样运行,但命令替换('$(...)')可能是一个问题。祝你好运。 – shellter

+0

作为一个单独的想法,在调用'contact = $(....)'之前添加'export destination'。祝你好运。 – shellter

+0

contact = sqlplus -s $ {SQLPLUS_INFO} << EOF SET PAGESIZE 0 SELECT email FROM table WHERE partner ='$ {destination}'; 退出 EOF 没有工作,以及,它返回: 行73:语法错误:文件意外结束 只是用()没有工作,以及,返回: 语法错误附近意外的标记' <<' 。 。 PS:导出也没有工作 @shellter,@Emmanuel –

回答

3

当您运行bash -x脚本会发生什么?我问,因为这里的文档符号期望在行的开头找到结束标记。当我运行这段代码:

#!/bin/bash 

    contact=$(cat - <<EOF 
    input from here document 
    second line 
    EOF 
    ) 

echo "$contact" 

我得到这样的错误:

eof.sh: line 3: unexpected EOF while looking for matching `)' 
eof.sh: line 10: syntax error: unexpected end of file 

如果线条与标签开始,你可以使用破折号档案结尾标记之前,以表明引出凸片应该被忽略。

#!/bin/bash 

     contact=$(cat - <<-EOF 
     input from here document 
     second line 
     EOF 
     ) 

echo "$contact" 

此输出:

input from here document 
second line 

替换空白的标签,你回语法错误。虽然我已根据bash来处理这个问题,但我相信你也会遇到与Korn和Bourne shell相同的问题。

所以,我怀疑你的问题与代码中的here-document格式有关,但你应该看到某种错误,所以我有点困惑。你应该得到你想要的进行替换:

#!/bin/bash 

description="The Description" 

     contact=$(cat - <<-EOF 
     input from here document 
     second line with '$description' embedded. 
     EOF 
     ) 

echo "$contact" 

这产生了:

input from here document 
second line with 'The Description' embedded. 

使用bash -x可以帮助追踪命令的执行。

所有这些只是巧合地与Oracle和SQL * Plus相关。

+0

我想我发现使用bash -x +回声问题 '目的地:X' 目的地:X +案 “$型” +回波 '类型是出站:2' 类型是出站:2 + + SQLPLUS -s用户/通行证@ EDDB +接触= ' 没有选择的行' +回声 +回声没有选择 任何行的行选择 +回声 该查询返回大量的空间: +回波'Destination:x'<<<<<空格 我设置:上 设定页面大小0 集COLSEP# 集LINESIZE 1000 集trimspool但看起来没有工作。 –

0

请尝试删除变量周围的单引号:它们阻止您的变量被解释(与双引号相反)。

这应该是:

SELECT email FROM table WHERE partner = ${destination}; 
+0

现在它返回: SELECT email FROM table WHERE partner = X; script.sh result.csv SELECT.sql错误在第1行:ORA-00904:“X”:无效标识符 没有工作,但感谢您的建议。 –

+1

SQL需要将文本值用单引号括起来。 – shellter

相关问题