bash - Random syntax error!? associative array expansion with a key which contains "operand" -
all
i got 2 2d-array files read bash
file1.txt (nx6)
desc1 dm1 w1 s1 critical1 grade1 #this line dosen't exist eee 1x 9 6 y e2 aaa x 1 2 y e2 c+c x 8 1 y t1 hhh 1x 5 5 y t2 iii x 5 5 y s1 jjj x 5 5 y s1 d+ x 2 3 y t1
file2.txt (mx3)
desc2 w2 s2 #this line dosen't exist aaa 1 2 eee 9 5 fff 10 7 ggg 4 9 iii 5 5 c+c 8 1 d+ 2 3 jjj 5 5
what wanna extract elements inside both files comparisons following picture:
i wanna in green labels take 1 element in $desc1 , compare whole elements in ${desc2[@]}, if does/dosen't find element in ${desc2[@]} feedback true/false
here work:
#!/bin/bash clear esc=`printf "\033"` ##===================================================================## ##===================================================================## ##========== read information file1.txt , file1.txt ==========## ##===================================================================## ##===================================================================## idx1=0 while read -a file1array$idx1; let idx1++ done < file1.txt idx2=0 while read -a file2array$idx2; let idx2++ done < file2.txt ##===================================================================## ##===================================================================## ##================ start compare these 2 files =================## ##===================================================================## ##===================================================================## ((i=0; i<idx1; i++)); ((j=0; j<idx2; j++)); desc1="file1array$i[0]" dm1="file1array$i[1]" w1="file1array$i[2]" s1="file1array$i[3]" critical1="file1array$i[4]" grade1="file1array$i[5]" desc2="file2array$j[0]" w2="file2array$j[1]" s2="file2array$j[2]" [ $i == 0 ] && lossarray+=(["$j"]="${!desc2}") if [[ "${!grade1}" == [e-gt-z][1-9] && "${!desc1}" == "${!desc2}" ]]; w1_judge=`expr "scale=3; ${!w1} - ${!w2}" | bc` s1_judge=`expr "scale=3; ${!s1} - ${!s2}" | bc` [ $w1_judge != 0 -o $s1_judge != 0 ] && declare -a jgws=( ["${!desc1}"]="wsng" ) elif [[ "${!grade1}" == [e-gt-z][1-9] && "$j" == `expr $idx2 - 1` ]]; [[ -z $(echo "${lossarray[@]}" | grep -o "${!desc1}") && "${!critical1}" != "null" ]] && declare -a jgloss=( ["${!desc1}"]="lossng" ) elif [[ "${!grade1}" != [e-gt-z][1-9] && "${!desc1}" == "${!desc2}" ]]; [[ "${!dm1}" == [1-2]x* || "${!dm1}" == "x" ]] && declare -a jgextra=( ["${!desc1}"]="extrang" ) fi done if [[ "${jgws[${!desc1}]}" == "wsng" ]]; echo "${!desc1}: ${esc}[0;31mwidth or space ng${esc}[0m" elif [[ "${jgloss[${!desc1}]}" == "lossng" ]]; echo "${!desc1}: ${esc}[0;31mloss ng${esc}[0m" elif [[ "${jgextra[${!desc1}]}" == "extrang" ]]; echo "${!desc1}: ${esc}[0;31mextra ng${esc}[0m" else echo "${!desc1}: ok" fi done
it's ok proceed whole script , output following result:
eee: width or space ng aaa: ok c+c: ok hhh: loss ng iii: ng jjj: ng d+: ok
but if change file1.txt this(take line "d+" line1):
d+ x 2 3 y t1 eee 1x 9 6 y e2 aaa x 1 2 y e2 c+c x 8 1 y t1 hhh 1x 5 5 y t2 iii x 5 5 y s1 jjj x 5 5 y s1
i got following result:
d+: syntax error: operand expected (error token "+")
how can solve problem if don't wanna edit file1.txt?
how can read 1st column of file2.txt array don't need in for-loop?
the problem bash throws error if try access element of undeclared array using +
. example:
$ echo "${arr[d+]}" -bash: d+: syntax error: operand expected (error token "+") $ echo "${arr[foo]}" # works
however, if declare array first, works:
$ declare -a arr $ echo ${arr[d+]} # works
so, need make sure arrays jgws
, jgloss
, jgextra
have been declared front, before access them in if-statements.
for example, jgws
array declared if condition met: [ $w1_judge != 0 -o $s1_judge != 0 ]
. later on, try use array here: if [[ "${jgws[${!desc1}]}" == "wsng" ]]
, might not have been initialised, in case fails.
Comments
Post a Comment