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

时间:2013-06-28 16:27:16

标签: oracle shell sqlplus

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
                ;;

根据上面的代码,如何将$ destination中的值传递给查询?上面的例子不起作用,即使是其他的例子:

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

2 个答案:

答案 0 :(得分:3)

使用bash -x运行脚本会发生什么?我问,因为here-document表示法希望在一行的开头找到结束标记。当我运行此代码时:

#!/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相关。

答案 1 :(得分:0)

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

应该是:

SELECT email FROM table WHERE partner = ${destination};
相关问题