2017-11-10 85 views
0

我試圖調試我的腳本,我是新的bash,我不明白錯誤 我想嘗試多次改變它,我標記爲60行 錯誤: 錯誤:./scriptdemo.txt:線60:results_array:壞數組下標壞陣列下標錯誤

#!/bin/bash 

echo the script is running 

#this part uses preCourses script to get preCourse demends for the input 
course 
results_array='' 
reqursion_counter=-2 

result_courses_counter=0 

function FindPreCourses { 

let reqursion_counter++ 
i=0 

grep $1 courses.list>file1 
while read line; do 

res=$(echo $line|grep $1) 

arr_line[$i]=$res 
let i++ 
done<file1 

rm file1 
j=0 

while (($j<${#arr_line[*]})) 

do 
read -a array_of_words<<<${arr_line[$j]} 

length_of_line=${#array_of_words[*]} 

if [[ 10#"$1" -eq 10#"${array_of_words[0]}" ]]; then 
    temp_line=${array_of_words[*]} 
fi 
let j++ 
done 

read -a line_of_course<<<$temp_line 
k=0 

len=${#line_of_course[*]} 

mistake_chk=1 

if (($mistake_chk==1)); then 
while ([[ ${line_of_course[$len-$k-1]} = +([0-9]) ]])&& 
(((10#${line_of_course[$len-$k-1]} > 10))) 
do 
results_array[$result_courses_counter]=${line_of_course[$len-$k-1]} 
let result_courses_counter++ 

let k++ 

done 

if (($reqursion_counter < $result_courses_counter-1)); then 

    FindPreCourses ${results_array[$reqursion_counter]} 

fi 
fi 
} 

FindPreCourses $1 
final=($(printf "%s\n" "${results_array[@]}" | sort -n)) 

printf '%s\n' "${final[@]}"|uniq 



} 

#this function searching for the sign students at the right semester 

function looking_for_students { 
grep $2 *.course>info2 
local counter=0 
while read line 
do 
    grep $2 *.course | read -a line | echo ${line[0]}>fileName:ID 
    let counter++ 
done < info2 
echo the number of the students counter was signing at $2 semester is 
$counter 
} 
+3

請看看:http://www.shellcheck.net/ – Cyrus

+0

'results_array'不是數組,它是一個字符串:'results_array =''' – Barmar

+0

A [mcve]隔離導致原因的最短代碼同樣的問題會對這個問題產生很大的影響。 –

回答

1

的主要問題是,您已定義results_array作爲一個正常的變量,但是使用它,就好像它是一個數組。在bash數組中有單獨的聲明語法。

使用此聲明空數組:

results_array=() 

declate -a results_array 

儘管這樣,也有一些完全不同的問題與您的代碼,並作爲@Cyrus建議,你應該有看看shellcheck來檢查你的語法。