Variable not found error in while loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Variable not found error in while loop
# 1  
Old 03-27-2015
Variable not found error in while loop

I am unable to use the value of a variable.
Code:
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
SmilieSmilie

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
Variable not found error in while loop-dbwhileloop-issuejpg

Last edited by TomG; 03-27-2015 at 11:36 AM..
# 2  
Old 03-27-2015
Code:
LstFldDataPart =`head -n "$LstFldDataPartCntr" "${FeedFileDir}/${FeedFileBadRecs}" | tail -1 |  tr -d '\n'`

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

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

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

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

This User Gave Thanks to Corona688 For This Post:
# 3  
Old 03-27-2015
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.

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

This User Gave Thanks to Corona688 For This Post:
# 4  
Old 03-30-2015
Hi Corona688,

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

Thank You for the valuable suggestions
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Continue the loop if the value is not found

Dear Help, Is it possible to continue the loop by going to the next available value, if the 'expected value' is not found. I have a list of values which might not get incremented by fixed value and hence the loop could break and the script could terminate. Any suggestion is appreciated. ... (1 Reply)
Discussion started by: Indra2011
1 Replies

2. UNIX for Dummies Questions & Answers

Variable not found error

Hi, I get a "FILEPATH: not found" error on the 3rd line and the line where it is within the case. Any idea as to why I'm getting this error time and time again? Oh and FILEPATH will store the directory of the file. I appreciate any help! IAM=`basename $0` RC=0 FILEPATH = ""... (1 Reply)
Discussion started by: MIA651
1 Replies

3. Shell Programming and Scripting

while loop error. (command not found)

can any1 please tell me what is problem with following code: i=1; cat test| while read CMD; do Var$i=$CMD; or Var$i=$(echo $CMD) ; let i++ doneI keep getting error : line 4: Var1=sometext: command not found (2 Replies)
Discussion started by: kashif.live
2 Replies

4. Shell Programming and Scripting

Syntax error: Bad for loop variable

I'm getting an error while running this script. Need help. set -x verbose #echo on clear #clear the screen USERNAME="bbb" PASSWORD="password" SERVER="192.168.1.100" WAIT_TIME=300 FILE_PATH="/home/users/xxx/MMM" # local directory to pickup *.dat file REMOTE_PATH="/Drop_off/xxx/yyy" #... (17 Replies)
Discussion started by: clgz2002
17 Replies

5. Shell Programming and Scripting

Variable not found error for a variable which is returned from stored procedure

can anyone please help me with this: i have written a shell script and a stored procedure which has one OUT parameter. now i want to use that out parameter as an input to the unix script but i am getting an error as variable not found. below are the unix scripts and stored procedure... ... (4 Replies)
Discussion started by: swap21783
4 Replies

6. Shell Programming and Scripting

Syntax error: Bad for loop variable

Hi Can any one help, I'm trying to run a script that beeps out the ip address from the PC internal speaker with the following script. It keeps throwing the error "Syntax error: Bad for loop variable" on line 16. I know its picking up the IP ADDRESS correctly. Any ideas on whats wrong. I'm... (3 Replies)
Discussion started by: dman
3 Replies

7. UNIX for Dummies Questions & Answers

Loop on array variable returning error: 0 not found

I'm guessing i have a syntax error. I'm not sure it get's past the the while condition. I get an error 0 not found. Simple loop not sure what I'm doing wrong. #!/usr/bin/ksh set -A MtPtArray /u03 /u06 tUbound=${#MtPtArray } echo $tUbound i=0 while ($i -lt $tUbound) do print... (4 Replies)
Discussion started by: KME
4 Replies

8. Shell Programming and Scripting

Loop through between 2 found lines

Hi frnd , hv nice week ahead, in shell script i found 2 words as below 1 ) targ_found=`sed -n \`echo $line_no\`p sesslog.txt | grep -ic 'Targ Rowid'` 2 ) bracket_found=`sed -n \`echo $line_no\`p sesslog.txt | grep -wc ')'` now we got he line number of 2 words. now i want to... (1 Reply)
Discussion started by: Gopal_Engg
1 Replies

9. Shell Programming and Scripting

file not found error during loop

Hi, I have a strange problem and I'm sure its because I'm doing something stupid. When I loop through all of the files in a directory they cannot be found. Any help would be greatly appreciated. Thanks, -E $ touch a.txt $ touch b.txt $ ls a.txt b.txt $ for f in `ls` ; do... (3 Replies)
Discussion started by: earls
3 Replies

10. Shell Programming and Scripting

variable not found with loop?

I cannot seem to pass the value of the variable assigned in the "else" statment below to an "if" statement outside of the loop. When I debug using "set -x", I find that the value of "$A" is empty. Is there something I'm missing here? Code is below: do if ] then echo "Good Match"... (2 Replies)
Discussion started by: douknownam
2 Replies
Login or Register to Ask a Question