# Variable not found error in while loop

**URL:** <https://community.unix.com/t/variable-not-found-error-in-while-loop/351520>\
**Category:** Shell Programming and Scripting\
**Created:** [March 27, 2015, 10:29am UTC](https://community.unix.com/t/variable-not-found-error-in-while-loop/351520 "2015-03-27T10:29:49Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![TomG](https://community.unix.com/user_avatar/community.unix.com/tomg/32/3104_2.png) [@TomG](https://community.unix.com/u/TomG)\
**Post date:** [March 27, 2015, 10:29am UTC](https://community.unix.com/t/variable-not-found-error-in-while-loop/351520/1 "2015-03-27T10:29:49Z")

</div>

I am unable to use the value of a variable.

```nohighlight
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  
😕😕

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

---

<div class="post-metadata">

**Author:** ![Corona688](https://community.unix.com/letter_avatar/corona688/32/6_22734026db12803ebb3e059622ac3534.png) [@Corona688](https://community.unix.com/u/Corona688)\
**Post date:** [March 27, 2015, 10:43am UTC](https://community.unix.com/t/variable-not-found-error-in-while-loop/351520/2 "2015-03-27T10:43:09Z")

</div>

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

```

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

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

```

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

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

```

---

<div class="post-metadata">

**Author:** ![Corona688](https://community.unix.com/letter_avatar/corona688/32/6_22734026db12803ebb3e059622ac3534.png) [@Corona688](https://community.unix.com/u/Corona688)\
**Post date:** [March 27, 2015, 10:46am UTC](https://community.unix.com/t/variable-not-found-error-in-while-loop/351520/3 "2015-03-27T10:46:38Z")

</div>

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.

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

```

---

<div class="post-metadata">

**Author:** ![TomG](https://community.unix.com/user_avatar/community.unix.com/tomg/32/3104_2.png) [@TomG](https://community.unix.com/u/TomG)\
**Post date:** [March 30, 2015, 1:32am UTC](https://community.unix.com/t/variable-not-found-error-in-while-loop/351520/4 "2015-03-30T01:32:04Z")

</div>

Hi Corona688,

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

Thank You for the valuable suggestions
