我正在尝试从 location.txt 中提取目录位置并将其保存到一个数组中,但它看起来不太好。当我执行脚本时,
我收到这个错误
./test.sh: line 7: location.txt: No such file or directory bina@ajax $ ./test.sh
./test.sh: line 5: =/apps/IBM: No such file or directory
./test.sh: line 5: =/usr/home: No such file or directory
./test.sh: line 11: Unix_Array[0]: command not found
./test.sh: line 11: Unix_Array[1]: command not found
这是我的代码
#!/bin/sh
counter=0
while read -r line; do
${Unix_Array[${counter}]}=$line;
let counter=counter+1;
done < location.txt
for ((i=0 ;counter > i; i++))
do
echo $(Unix_Array[$i])
done
和文本文件 位置.txt
/apps/IBM
/usr/home
/var/login
谁能告诉我我做错了什么? 我可能犯了一个我不知道的愚蠢错误......(我真的是 UNIX 新手)
提前致谢
最佳答案
只需删除 ${Unix_Array[${counter}]}=$line;
上的第一个 ${}
,因为您只想分配给数组,而不是分配 和 检索它的值,您的固定脚本将是这样的:
#!/bin/sh
counter=0
while read -r line; do
Unix_Array[${counter}]=$line;
let counter=counter+1;
done < location.txt
for ((i=0 ;counter > i; i++))
do
echo ${Unix_Array[$i]}
done
关于linux - 将文本行保存到数组时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12628200/