我正在尝试将 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[@]}"
这是我得到的错误:
我知道问题出在我的运算符(operator) % 上,但我需要它来限制我的查询结果。
最佳答案
这听起来像 this very specific ksh bug其中 here-documents 默默地将单引号转换为双引号。您可以尝试该答案中的解决方法,例如
#!/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[@]}"
关于sql - 将 SQL 结果传递到 shell 脚本中的数组时标识符无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53191195/