linux - 将文本行保存到数组时出错

标签 linux bash shell aix

我正在尝试从 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/

相关文章:

linux - bash 中的 hex 到 bin 文件转换

linux - shell脚本中调用函数

linux - 使用 shell 脚本对文件进行排序然后删除旧文件

Python 和子进程中的别名

mysql - 通过SSH将数据库表单独导出到sql文件

c# - 为我的应用程序制作属性处理程序以将自定义属性添加到文件格式

linux - 具有多个 Remote 和操作顺序的 git 工作流程

linux - 根据unix中的模式将单行拆分为多行

linux - 如何在文件中压缩-1

bash——基于特定列的两个文件的交集?