gpt4 book ai didi

sql - 将 SQL 结果传递到 shell 脚本中的数组时标识符无效

转载 作者:太空宇宙 更新时间:2023-11-04 09:12:36 25 4
gpt4 key购买 nike

我正在尝试将 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[@]}"

这是我得到的错误:

Screenshot of the error i get

我知道问题出在我的运算符(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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com