gpt4 book ai didi

linux - 在 bash 中将条目添加到数组

转载 作者:太空狗 更新时间:2023-10-29 11:27:31 25 4
gpt4 key购买 nike

在 bash 中,我尝试创建一个数组,然后多次运行循环(由文件的用户确定),然后按预定次数向该数组添加一个选项。这是贸易数据,例如,我选择 2 作为因子。然后程序让我输入我想要的因素,我输入 open(当天的开盘价),然后将 bid 添加到数组 arr 并再次提出问题。然后我输入收盘价(当天的收盘价)然后将收盘价添加到数组中,最后arr = 那样打开关闭。但是我运行代码和问题:"How many factors would you like to check total:" 只是一遍又一遍地运行,我从不离开循环,而且从来没有出现输入被放置的情况进入数组。非常感谢对我在这里的错误的任何帮助。谢谢。

   factor=""
total=0
declare -a arr


read -p "How many factors would you like to check total: " -e -i "$total" total

for (( x=1; x=total; x++ ))
do
read -p "Enter factor from list: " -e -i "$factor" factor
arr+=(${arr[@]} "$factor")
done

echo ${arr[@]}

最佳答案

你几乎在数组追加上做对了。请记住,+= 运算符不需要在 RHS 上再次完整引用数组。例如只是

arr+=($factor)

在数组变量 arr 的末尾附加 $factor 就足够了。

像这样修改你的脚本:

factor=""
total=0
declare -a arr

read -p "How many factors would you like to check total: " -e -i "$total" total

for (( x=1; x<=total; x++ ))
do
read -p "Enter factor from list: " -e -i "$factor" factor
arr+=($factor)
done

echo ${arr[@]}

关于linux - 在 bash 中将条目添加到数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16469351/

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