【问题标题】:Invalid identifier when passing SQL result into an array in a shell script将 SQL 结果传递到 shell 脚本中的数组时标识符无效
【发布时间】:2019-04-10 23:38:22
【问题描述】:

我正在尝试将 SQL 查询结果存储到 shell 脚本中的数组中,但在运行 .sh 时遇到了无效标识符错误

您能检查一下我的代码中有什么问题吗?

#!/usr/bin/ksh

echo Start Executing SQL commands

array=$(sqlplus -s apps/apps << eof
SET PAGESIZE 0;
SELECT directory_name from all_directories where directory_name like '%XXBP%';
eof)


printf '%s\n' "${array[@]}"

这是我得到的错误:

我知道问题来自我的操作员 % 但我需要它来限制我的查询结果。

【问题讨论】:

  • 我看到类似搜索字段 '%XXBP%' 但在错误消息中 "%XXBP%" 双引号就是问题所在......它应该是单引号
  • 如上所示,我用单引号编写了代码
  • 哇。这是一个主要的 ksh88 错误,使用 ksh93 的充分理由。顺便说一句,您的数组变量未声明为数组(typeset -a),因此 ${array[@]} 给出了变量的内容。 ${array} 或 $array 也可以。

标签: sql linux oracle shell sqlplus


【解决方案1】:

这听起来像this very specific ksh bug,这里的文档会默默地将单引号转换为双引号。您可以尝试该答案中的解决方法,例如

#!/usr/bin/ksh

echo Start Executing SQL commands

# put the single-quotes in a variable to prevent the here-document from converting them to double
STR="'%XXBP%'"

array=$(sqlplus -s apps/apps << eof
SET PAGESIZE 0;
SELECT directory_name from all_directories where directory_name like $STR;
eof)

printf '%s\n' "${array[@]}"

【讨论】:

  • 谢谢它似乎有效。我只是不明白为什么
猜你喜欢
  • 1970-01-01
  • 2016-12-01
  • 2022-09-27
  • 1970-01-01
  • 2019-02-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-06
相关资源
最近更新 更多