What is wrong with the following expression:
...
if [ $i -ge ${tLen} ]; then
echo "Maximum limit reached, i=$i"
fi
i=$(($i+1))
...
This expression is inside a while loop.
i is the counter variable, whose initial value is 0.
tLen stores the length of the array, whome I am traversing.
This condition is not getting true even when i reaches tLen. Where am I going wrong?
This is my complete code :
read filename
words=(5 2)
i=0
sed 's/ /\n/g' "$filename" >"tmp.txt"
while read word
do
words[i]=$word
i=$(($i+1))
awk "/$word/ {count += 1} END{print count}" "tmp.txt" >>"tmp2.dat"
done <"tmp.txt"
i=0
tLen=${#words[@]}
echo "Length of words: ${tLen}"
declare -A wordMap
while read count
do
if [ $i -ge ${tLen} ]; then
echo "Maximum length reached, i=$i"
break
fi
wordMap["${words[$i]}"]=$count
i=$(($i+1))
done <"tmp2.dat"
rm "tmp.txt"
rm "tmp2.dat"
Actually I am trying to calculate the frequency of each word in a given text…
The code would be problematic for texts that have empty lines in them, or multiple spaces between the words. In that case the line
would fail with a
bad array subscriptmessage because${words[$i]}is empty.