Variable not found error in while loop

I am unable to use the value of a variable.

while [[ $LstFldDataPartFldCnt -eq 1 ]]
				do
					LstFldDataPart =`head -n "$LstFldDataPartCntr" "${FeedFileDir}/${FeedFileBadRecs}" | tail -1 |  tr -d '\n'`
					echo $LstFldDataPart
					JndLstFldDataPart="${JndLstFldDataPart}${LstFldDataPart}"
					LstFldDataPartCntr= `expr $LstFldDataPartCntr + 1`
					LstFldDataPartFldCnt=`head -n "$LstFldDataPartCntr"  "${FeedFileDir}/${FeedFileBadRecs}" | tail -1 |  tr -d '\n'| awk -F "|" '{print NF}'`	
					exit
				done

The Problematic variable is --->LstFldDataPart
I tried to echo the data of this variable. its showing error
:confused::confused:

I have intentionally included exit in this while loop for debugging purpose.

I am using HP-UX,Korn shell

Please help me to identify the root cause.

PFA screenshot of while loop debug

Thanks

LstFldDataPart =`head -n "$LstFldDataPartCntr" "${FeedFileDir}/${FeedFileBadRecs}" | tail -1 |  tr -d '\n'`

is wrong, because of the spaces around the equal. Try:

LstFldDataPart=`head -n "$LstFldDataPartCntr" "${FeedFileDir}/${FeedFileBadRecs}" | tail -1 |  tr -d '\n'`

Though I would try and replace that entire pipe chain with one awk:

LstFldDataPart=`awk -v C="$LstFldDataPartCntr" 'NR==C { printf("%s", $0); exit }' "${FeedFileDir}/${FeedFileBadRecs}"`
1 Like

Actually, if your loop is doing what I think it is -- running one head, tail, awk and tr for each line in a file -- that is extremely inefficient, and should be replaced entirely with a while read loop which just reads lines directly instead of scrubbing the entire file through 3 external utilities each loop to do so.

read also has the useful feature of splitting on tokens. It can split on | for you without calling any external programs either. It works best with a predictable number of fields, since if there's more fields than variables, the last n extra fields get lumped in the last one you list.

while IFS="|" read -r VARA VARB VARC VARD VARE
do
        echo "D is $VARD"
done < inputfile
1 Like

Hi Corona688,

Thank You so much , Removal of space before and after the = helped.

Thank You for the valuable suggestions