gpt4 book ai didi

arrays - 括号问题和使用 sqlplus 和 Shell 脚本声明数组

转载 作者:行者123 更新时间:2023-12-03 08:28:59 31 4
gpt4 key购买 nike

在我的脚本中创建变量时,我收到“ 语法错误:“(”意外(预期“}”)”错误。我希望从 sqlplus 获取输出并将其作为数组返回。这是我正在使用的功能:

get_list() {
ip=$1
port=$2
db_user=$3
db_pass=$4

names_array=(`sqlplus -S $db_user/$db_pass@$ip:$port << EOF
set heading off
set feedback off;
select username from users;
EOF`)

element=0
while [ $element -lt ${names_array[*]} ]
do
echo ${element}") "${names_array[$element]}
let element=$element+1;
done
printf "\n\n"
read -p "Choice:" selection

sqlplus -S $db_user/$db_pass@$ip:$port << EOF
drop user ${names_array[$selection]};
quit;
EOF


}

当我在这里声明数组时发生错误。这是 bash 提示的括号:
names_array=(`sqlplus -S $db_user/$db_pass@$ip:$port << EOF

任何帮助表示赞赏。提前致谢。

最佳答案

某些 shell(sh、dash)不支持数组。使用支持它们的 bash。

我可以在破折号中复制错误:

arr=(`cat << EOF
a
b
c
EOF`)
dash: 11: Syntax error: word unexpected (expecting ")")

在 bash 中,代码可以正常工作:
arr=(`cat << EOF
a
b
c
EOF` )
echo ${arr[1]}

输出:
b

关于arrays - 括号问题和使用 sqlplus 和 Shell 脚本声明数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29433906/

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