Arrays Bash数组操作
我正在为一个Unix/Linux类做一个项目,在另一个文件中有一些数组,如下所示 所有数组应存储为1或0Arrays Bash数组操作,arrays,linux,bash,unix,Arrays,Linux,Bash,Unix,我正在为一个Unix/Linux类做一个项目,在另一个文件中有一些数组,如下所示 所有数组应存储为1或0 T1=( 1 1 1 1 1 ) T2=( 1 1 1 1 1 ) T3=( 1 1 1 1 1 ) T4=( 1 1 1 1 1 ) T5=( 1 1 1 1 1 ) 但是,我很难将数组编辑为0并使更改保持不变 #!/bin/bash i=0 for line in `cat JeopardyOut` do
T1=( 1 1 1 1 1 )
T2=( 1 1 1 1 1 )
T3=( 1 1 1 1 1 )
T4=( 1 1 1 1 1 )
T5=( 1 1 1 1 1 )
但是,我很难将数组编辑为0并使更改保持不变
#!/bin/bash
i=0
for line in `cat JeopardyOut`
do
let i=i+1
#Creating one big Array with all the values from the arrays file
Array[i]=$line
done
cat JeopardyOut
#Parsing the giant Array into the main 5 arrays I'm using
createArray()
{
y=0
for y in 0 1 2 3 4
do
for i in 2 3 4 5 6
do
#echo "${Array[i]}"
T1[$y]=${Array[i]}
done
for i in 9 10 11 12 13
do
#echo "${Array[i]}"
T2[$y]=${Array[i]}
done
for i in 16 17 18 19 20
do
#echo "${Array[i]}"
T3[$y]=${Array[i]}
done
for i in 23 24 25 26 27
do
#echo "${Array[i]}"
T4[$y]=${Array[i]}
done
for i in 30 31 32 33 34
do
#echo "${Array[i]}"
T5[$y]=${Array[i]}
done
done
}
createArray
ArrayNum=$1
Index=$2
也许有更好的方法可以做到这一点,但这正是最终对我有效的方法
#Changing the necessary indexes, this will be used by a completely
#different script
ChangeArray()
{
if [[ $ArrayNum == "1" ]]; then
T1[ $Index ]=0
elif [[ $ArrayNum == "2" ]]; then
T2[ $Index ]=0
elif [[ $ArrayNum == "3" ]]; then
T3[ $Index ]=0
elif [[ $ArrayNum == "4" ]]; then
T4[ $Index ]=0
elif [[ $ArrayNum == "5" ]]; then
T5[ $Index ]=0
else
echo "Invalid Parameters"
fi
}
if [[ $ArrayNum -ne "" || $Index -ne "" ]]; then
if [[ $ArrayNum == "5" && $Index == "5"]]; then
reset
else
ChangeArray
fi
fi
# And the part that's likely at fault for my issue but don't know how I
# should fix it
echo "T1=( ${T1[*]} )" > JeopardyOut
echo "T2=( ${T2[*]} )" >> JeopardyOut
echo "T3=( ${T3[*]} )" >> JeopardyOut
echo "T4=( ${T4[*]} )" >> JeopardyOut
echo "T5=( ${T5[*]} )" >> JeopardyOut
cat JeopardyOut
我试图编辑数组的方式有问题。。。
虽然我可以将任何数组的任何索引设置为0,但我不知道为什么在重新运行脚本时,我更改为0的1会变回1
注:这是Sierra中Linux编程的一个基础类,除了我通过反复试验学到的内容外,我对bash脚本的理解并不多。也许这个示例可以帮助您
#!/bin/bash
while read -r line; do
#Creating one big Array with all the values from the arrays file
Array+=(`grep -oP '\(\K(.+?)(?=\))' <<< "$line"`)
done < JeopardyOut
echo "Giant Array value: ${Array[@]}"
# Or you can clone the arrays in the file
i=0
while read -r line; do
((i++))
eval "T$i=(`grep -oP '\(\K(.+?)(?=\))' <<< "$line"`)"
done < JeopardyOut
echo "This is the contents of array T1: ${T1[@]}"
echo "This is the contents of array T2: ${T2[@]}"
echo "This is the contents of array T3: ${T3[@]}"
echo "This is the contents of array T4: ${T4[@]}"
echo "This is the contents of array T5: ${T5[@]}"
# This can help you to make a new file
# I change some value to our arrays
T1[2]=0
T2[1]=true
# Now let's go to make a new arrays file
echo "T1=(${T1[@]})" > JeopardyOut2
echo "T2=(${T2[@]})" >> JeopardyOut2
echo "T3=(${T3[@]})" >> JeopardyOut2
echo "T4=(${T4[@]})" >> JeopardyOut2
echo "T5=(${T5[@]})" >> JeopardyOut2
echo "This is the content of JeopardyOut2:"
cat JeopardyOut2
bash
确实不是执行数组操作的理想语言;数组旨在作为传递参数的第二级引用,而不是作为通用数据结构。您能否给出一个示例,说明调用createArray
后,Array
将是什么样的,以及生成的T
数组应该是什么样的?您需要回到开头,找到一个解决方案来处理文件中的一行。开始时的for循环毫无意义。尝试只运行for循环并在$line的值处回显。。。你可能会对你得到的结果感到惊讶(这是假设文件的格式是问题顶部的格式)eval
在这里几乎肯定是不必要的。感谢上帝,谢谢你,一个漫长的通宵,呃,你救了我一命。我想我找到了问题,但解决它是另一回事。我在表达上有一个弱点。我需要一些时间来理解你在那里所做的一切,但这是一个很大的帮助。再次感谢。我不明白downvotation。@chepner我必须使用eval,因为我不能用另一种方式将数字分配给数组名。当然可以:停止使用循环来设置5个变量。
$ bash example
Giant Array value: 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
This is the contents of array T1: 1 1 1 1 1
This is the contents of array T2: 1 1 1 1 1
This is the contents of array T3: 1 1 1 1 1
This is the contents of array T4: 1 1 1 1 1
This is the contents of array T5: 1 1 1 1 1
This is the content of JeopardyOut2:
T1=(1 1 0 1 1)
T2=(1 true 1 1 1)
T3=(1 1 1 1 1)
T4=(1 1 1 1 1)
T5=(1 1 1 1 1)
$