【问题标题】:Bash ending the variable after first space?Bash 在第一个空格后结束变量?
【发布时间】:2014-12-10 12:36:45
【问题描述】:

我正在循环中在 Bash 中构建一个字符串。循环处理的数据来自两个文件中的行,如下所示:

字符串的第一部分是文件 1 中的一行,如下所示:

SOME_PACKAGE

第二部分是文件 2 的一行,如下所示:

someFunction('some',parameters,here)

最终输出在两个字符串之间有一个点:

1 SOME_PACKAGE.someFunction('some',parameters,here)

1在这里很重要。一会儿解释。

字符串是在一个双 while 循环中形成的

while read line1 ; do
    while read line2 ; do
      stringArray=($line2)
      string=$line1.${stringArray[1]}

        sqlplus -s /nolog > /dev/null 2>&1  <<EOF
        connect user/password@db_instance
        variable rc refcursor;
        SPOOL ${line1}_${stringArray[0]}.DATA
        exec :rc := $string;
        print rc;
        spool off
        exit
        EOF

    done < file2.txt
done < file1.txt

这个字符串然后被传递给 SQLPlus,并且 SQLPlus 应该像这样退出一个命令:

SQL> variable rc refcursor;
SQL> exec :rc := SOME_PACKAGE.someFunction('some',parameters,here);
SQL> print rc;

到目前为止,一切正常。但是我在 someFunction 旁边得到了一个更复杂的参数。现在看起来像这样:

SOME_PACKAGE.someFunction('some',parameters,here,'and 2 more',NULL)

似乎传递给 SQL*Plus 的变量以第一个空格结束...所以看起来像:

SOME_PACKAGE.someFunction('some',parameters,here,'and

据我所知,我不应该在变量中传递空格,或者如果我想这样做,我应该将它们用引号括起来:"" 但是我应该把这些引号放在哪里以将最终变量传递给 SQL*Plus没有那些引号?或者你们有什么其他的解决方案?

【问题讨论】:

  • 听起来你的字段分隔符有问题。如果在嵌套的 while 循环中设置 IFS= 会怎样?
  • @arco444,真的,我问的问题是错误的,因为我忽略了一个非常重要的事实,我认为这根本不重要......让我更新这个问题。
  • @arco444 现在问题已经完成,但我认为IFS 可能是我的麻烦的答案...

标签: bash sqlplus


【解决方案1】:

感谢@arco444,答案很简单。

这一切发生的原因是内部字段分隔符,它被设置为默认值。

我做了以下事情:

我已将file2 的外观从

1 SOME_PACKAGE.someFunction('some',parameters,here,'and 2 more',NULL)

1§SOME_PACKAGE.someFunction('some',parameters,here,'and 2 more',NULL)

并且我在循环前后添加了一些ILS的变化,所以最终的代码是这样的:

oldifs=$IFS
IFS="§"

while read line1 ; do
    while read line2 ; do
      stringArray=($line2)
      string=$line1.${stringArray[1]}

        sqlplus -s /nolog > /dev/null 2>&1  <<EOF
        connect user/password@db_instance
        variable rc refcursor;
        SPOOL ${line1}_${stringArray[0]}.DATA
        exec :rc := $string;
        print rc;
        spool off
        exit
        EOF

    done < file2.txt
done < file1.txt

IFS=$oldifs

现在一切都像魅力一样运作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-17
    • 2011-08-14
    • 2020-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多