Variable inside while loop got reset

hi,

I am using hp unix server and not getting variable output present inside the while loop. I have tried changing the code and need to verify whether it is proper practice of code. I am expecting the output of varible RUN_FILE 3 to TRUE which i get inside the while loop.

RUN_FILE 1=TRUE
echo $RUN_FILE 1=$RUN_FILE"
for RECS_REC in ${RECS_TBL[@]}; do
find ${RILD_MARK_DIR}/*Rele**.${DATA_FILE_CUSTOMER}.**.${DATA_FILE_PALLET}.* -not -name '*.'"$RECS_REC" | while read rname; do
echo "$rname"
BCODE='SETUP FAILED'
echo "****************************************************************************"
echo "mv "$rname" ${RILD_PETA_DIR}/file_error"
mv "$rname" ${RILD_PETA_DIR}/file_error
RUN_FILE="TRUE"
echo "RUN_FILE 2=$RUN_FILE"
done
done
echo $RUN_FILE 3=$RUN_FILE"

Current Output

Output :
RUN_FILE 1= TRUE
RUN_FILE 2=TRUE
RUN_FILE 3=FALSE

Expected Output

Output :
RUN_FILE 1= TRUE
RUN_FILE 2=TRUE
RUN_FILE 3=TRUE

MY MODIFIED CODE

RUN_FILE 1=TRUE
echo $RUN_FILE 1=$RUN_FILE"
for RECS_REC in ${RECS_TBL[@]}; do
VAR1=/home/skk/test.txt
find ${RILD_MARK_DIR}/*Rele**.${DATA_FILE_CUSTOMER}.**.${DATA_FILE_PALLET}.* -not -name '*.'"$RECS_REC" >>/home/skk/test.txt
while read rname; do
echo "$rname"
BCODE='SETUP FAILED'
echo "****************************************************************************"
echo "mv "$rname" ${RILD_PETA_DIR}/file_error"
mv "$rname" ${RILD_PETA_DIR}/file_error
RUN_FILE="TRUE"
echo "RUN_FILE 5=$RUN_FILE"
done > $VAR1
rm -rf $VAR1
done
echo $RUN_FILE 3=$RUN_FILE"

I have modified the above code to reflect the variable present inside the while loop.
Not sure whether it is best code practices

Other than testing to see if we would notice that the code you have shown us could not possibly produce the output you have shown us, I'm not sure what you want us to do.

Inconsistent spacing, mismatched quotes, undefined variables all lead me to believe that the code you have shown us could not be run successfully by any shell on a BSD, Linux, or UNIX operating system.

Unless this is an entry in an obfuscated code contest, using a utility named RUN_FILE and using a variable named RUN_FILE is not a good coding practice.

If you have a problem and need our help, please:

  1. tell us what operating system and shell you're using,
  2. show us the actual code that you are running,
  3. show us the actual output produced when you run that code,
  4. and clearly specify what it is that you are trying to do, what is not working, and indicate where you are stuck trying to fix your code.

The difference between

find ${RILD_MARK_DIR}/*Rele**.${DATA_FILE_CUSTOMER}.**.${DATA_FILE_PALLET}.* -not -name '*.'"$RECS_REC" | while read rname; do

and

find ${RILD_MARK_DIR}/*Rele**.${DATA_FILE_CUSTOMER}.**.${DATA_FILE_PALLET}.* -not -name '*.'"$RECS_REC" >>/home/skk/test.txt
while read rname; do

is that a pipe forces a subshell to be created. The variable is set just fine in that subshell - but when the loop ends, the subshell ceases to exist.

This would work in ksh, which evaluates pipes in the opposite order, executing the last part in your own shell instead of the subshell.

Also, you may want to rethink >>/home/skk/test.txt which will make test.txt longer and longer every time you run it. Just >/home/skk/test.txt will replace the file with new results every run.

1 Like

Thanks for your response @Don.

Find my answers below.

tell us what operating system and shell you're using,

Linux 2.6.32-279.el6.x86_64 #1 SMP Fri Jun 22 12:19:21 UTC 2012 x86_64 x86_64 x86_64 GNU/Linux

show us the actual code that you are running,

RUN_FILE 1=FALSE
echo $RUN_FILE 1=$RUN_FILE"
for RECS_REC in ${RECS_TBL[@]}; do
find ${RILD_MARK_DIR}/*Rele**.${DATA_FILE_CUSTOMER}.**.${DATA_FILE_PALLET}.* -not -name '*.'"$RECS_REC" | while read rname; do
echo "$rname"
BCODE='SETUP FAILED'
echo "****************************************************************************"
echo "mv "$rname" ${RILD_PETA_DIR}/file_error"
mv "$rname" ${RILD_PETA_DIR}/file_error
RUN_FILE="TRUE"
echo "RUN_FILE 2=$RUN_FILE"
done
done
echo $RUN_FILE 3=$RUN_FILE"

show us the actual output produced when you run that code,

Output :
RUN_FILE 1= FALSE
RUN_FILE 2=TRUE
RUN_FILE 3=FALSE

and clearly specify what it is that you are trying to do, what is not working, and indicate where you are stuck trying to fix your code.

pipe forces a subshell to be created and once the loop completed.
the result of the variable which i got in inside the loop is reset after the loop.

Expecting the output to be

RUN_FILE 1= FALSE
RUN_FILE 2=TRUE
RUN_FILE 3=FALSE

The method you solved the problem is as good a way as any. You may wish to use mktemp for a safer temporary file name.

VAR1=`mktemp`

I asked what operating system and shell you're using. You have now shown us which OS you're using, but not which shell.

I asked what your shell script really was and what output was produced when you ran it. You have not done either of those. With the code you have shown us, running that code (assuming there is no utility in your PATH named RUN_FILE , that the variable RUN_FILE was not inherited in the environment, and that you ran that code using a shell that uses Bourne shell syntax) would produce output similar to:

tester: line 1: RUN_FILE: not found
1=
for RECS_REC in ; do
find /*Rele**..**..* -not -name '*.' | while read rname; do
echo 
BCODE='SETUP FAILED'
echo ****************************************************************************
echo mv  /file_error
mv  /file_error
RUN_FILE=TRUE
echo RUN_FILE 2=
done
done
echo  3=

If there is a utility named RUN_FILE in your PATH , a variable named RUN_FILE has been exported to your environment and initialized with a value similar to RUN_FILE , and you ran the script with a Bourne compatible shell, you'd get output similar to:

RUN_FILE 1= RUN_FILE
for RECS_REC in ; do
find /*Rele**..**..* -not -name '*.' | while read rname; do
echo 
BCODE='SETUP FAILED'
echo ****************************************************************************
echo mv  /file_error
mv  /file_error
RUN_FILE=TRUE
echo RUN_FILE 2= RUN_FILE
done
done
echo  RUN_FILE 3= RUN_FILE

neither of which look anything like the output you say that code produces.

You say: "pipe forces a subshell to be created and once the loop completed.
the result of the variable which i got in inside the loop is reset after the loop.", but there is no pipeline in the code you showed us. The only pipe symbol in your script is inside a double-quoted string that is printed by the first echo in your script.

There is no way on earth to create a shell script where the command:

echo $RUN_FILE 3=$RUN_FILE

is going to print:

RUN_FILE 3=FALSE

The expansion of $RUN_FILE could produce RUN_FILE if something in your script had set RUN_FILE=RUN_FILE , but there is no code anywhere in your script that does that. The expansion of $RUN_FILE could produce FALSE if something in your script had set RUN_FILE=FALSE , but there is no code anywhere in your script that does that. There is no way that two expansions of the same variable in a single echo statement will produce different results.

And, the command that was in your script:

echo $RUN_FILE 3=$RUN_FILE"

is only sort of doing what you might have wanted it to do because the mismatched double-quote at the end of the line is matching another mismatched double-quote character somewhere else in your script. (In this case it is matching the double quote at the end of line #2 in your script.)

If you won't tell us what shell you're using (including its release number), show us the actual code you're running, show us the actual output that your code is producing, and CLEARLY explain what you are trying to do that is not working; we are wasting our time trying to help you.

1 Like

thanks for your response don.

 
OS  : Linux
Shell : Bourne shell bash

Currently, the variable set inside the loop is not same as one the loop is completed. when i run in sh mode.

If i am running the program as ksh mode and it runs fine without any issues.
Previously it was running sh and while loop i got the variable as true and in the next variable it set to default one instead of the variable got inside the loop.

If i use ksh instead sh will there be any problem.

---------- Post updated at 06:51 AM ---------- Previous update was at 04:22 AM ----------

Thanks for your response.
If i run through ksh it is working fine.
Variable set as same even if the loop ends.

will it impact anything in the code if i run as ksh mode?

ksh usually has a superset of the sh commands, so a script running with sh should run with ksh , but some details may differ, esp. if your system's sh is an alias to e.g. bash .

As I said in post #6, it is obvious that you haven't shown us the code you are actually running and haven't shown us the output you get from the code you are running.

You tell us your code is working fine when run with ksh . I'll believe you when you say that. But if you say it works when you run it with ksh why are you asking me if it will work if you run it with ksh ? You have already said that it does. And, since I don't know what your code really looks like, I have absolutely no information that would allow me to make an informed opinion one way or another.

In the post I quoted above, you said your shell is "Bourne shell bash". There is a Bourne shell and there is bash . They are not the same thing. Although bash will run most Bourne shell scripts, there are many bash scripts that cannot be run by a Bourne shell .

I wish you luck.