我正在尝试用bash编写一个脚本,用七个变量“构建”一个数组。例如:
a=7
b=3
c=5
....
declare -a elem_arr=( "$a" "$b" "$c"..."$g" )
echo "$elem_arr[1]" # this doesn't echo anything.
if [ "$elem_arr[1]" -ne "$elem_arr[2] ]; then
echo "$elem_arr[1] is not equal to $elem_arr[2]"
fi
这似乎不起作用。是否有可能以这种方式构造一个数组?
答案 0 :(得分:-1)
检查出来
#! /bin/bash
a="This"
b="script"
c="works!"
count=1
for i in $( echo $a $b $c )
do
array[$count]=$i
count=$[ $count+1 ]
done
echo "${array[1]} ${array[2]} ${array[3]}"
此脚本通过使用for循环来单独定义数组中的每个元素。然后,您可以使用默认语法${array[index]}