我想写一个bash脚本,将得到用户input并将其存储在一个数组中。 input:1 4 6 9 11 17 22
我希望这被保存为数组。
像这样读取它:
read -a arr
read -a arr <<< "1 4 6 9 11 17 22"
打印数组中的元素:
echo ${#arr[@]}
或者通过上面的数组循环
for i in ${arr[@]} do echo $i # or do whatever with individual element of the array done
这个怎么样:
while read line do my_array=("${my_array[@]}" $line) done printf -- 'data%s ' "${my_array[@]}"
按Ctrl-D停止输入数字。
这是我的2美分。
#!/bin/sh read -p "Enter server names separated by 'space' : " input for i in ${input[@]} do echo "" echo "User entered value :"$i # or do whatever with individual element of the array echo "" done