2017-06-14 47 views
0

我有幾個文件與某些keywors(一個是MAT1)。 對於這個關鍵詞我喜歡讀取與它相對應的ID,並將其與文件名一起放入數組中。 我嘗試以下(我不是很熟悉bash編程):陣列在bash中創建時出錯

#!/bin/bash 
Num=0 
arr=($(find . -name '*.mat' | sort)) 

for i in "${arr[@]}" 
do 

    file=$(basename "${i}") 

    while read -r line 
    do 

    name="$line" 
    IFS=' ' read -r -a array <<< "$line" 
    for index in "${!array[@]}" 
    do 

     if [ ${array[index]} == "MAT1" ] 
     then 
     out[$num] = "${array[index+1]} $file " 
     let num++ 
     #printf "%-32s %8i\n" "$file" "${array[index+1]}" 
     fi 

    done 

    done < "$i" 

done 

有了這個得到的消息

make_mat_list.bsh:第21行:出來[0]:找不到命令

make_mat_list.bsh:line 21:out [1]:command not found

這裏有什麼問題?

回答

2

bash是白色空間敏感的,下面的行不能有空格。

​​

作爲該錯誤的原因,外殼對待該特定行中的第一個字是一個命令out[$num]out[1] ..等和其餘它作爲參數給它="${array[index+1]} $file ",不使任何意義。刪除空格,並刪除空格

out[$num]="${array[index+1]} $file"