Variable inside while loop got reset

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Variable inside while loop got reset
# 1  
Old 11-17-2017
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.

Code:
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

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

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

MY MODIFIED CODE

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

Last edited by gowthamsoft; 11-17-2017 at 08:29 AM..
# 2  
Old 11-17-2017
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.
# 3  
Old 11-17-2017
The difference between
Code:
find ${RILD_MARK_DIR}/*Rele**.${DATA_FILE_CUSTOMER}.**.${DATA_FILE_PALLET}.* -not -name '*.'"$RECS_REC" | while read rname; do

and
Code:
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.
This User Gave Thanks to Corona688 For This Post:
# 4  
Old 11-23-2017
Thanks for your response @Don.

Find my answers below.

tell us what operating system and shell you're using,
Code:
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,
Code:
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,
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.
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
Code:
RUN_FILE 1= FALSE
RUN_FILE 2=TRUE
RUN_FILE 3=FALSE

# 5  
Old 11-23-2017
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.

Code:
VAR1=`mktemp`

# 6  
Old 11-23-2017
Quote:
Originally Posted by gowthamsoft
Thanks for your response @Don.

Find my answers below.

tell us what operating system and shell you're using,
Code:
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,
Code:
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,
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.
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
Code:
RUN_FILE 1= FALSE
RUN_FILE 2=TRUE
RUN_FILE 3=FALSE

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:
Code:
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:
Code:
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:
Code:
echo $RUN_FILE 3=$RUN_FILE

is going to print:
Code:
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:
Code:
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.
This User Gave Thanks to Don Cragun For This Post:
# 7  
Old 12-06-2017
thanks for your response don.

Code:
 
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?

Last edited by gowthamsoft; 12-06-2017 at 08:05 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to write a Boolean variable which succeed and failed inside the if loop in shell script ?

I have if loop with multiple variable value check in if loop. How can i print only if loop satisfied variable and its value in shell script ? I dont want to check each variable in if loop. That makes my script larger. if ] then echo "Only satisfied variable with value" ... (3 Replies)
Discussion started by: prince1987
3 Replies

2. UNIX for Beginners Questions & Answers

Variable and awk inside for loop

Thanks all for taking time out and reading this thread and big Thanks to all who have come forward for rescue. Background: I have a variable "nbrofcols" that has number of columns from a data file. Now, using this count in for loop, I am trying to get the maximum length of each column present... (7 Replies)
Discussion started by: svks1985
7 Replies

3. Shell Programming and Scripting

Reset $1 variable

Hi all, I'm using a scipt with one input and one output. I'm referring to the input by $1 after executing a command line, I'm getting the output via $1. Normally, the $1 shouldn't get the same values between the first call and the second. Is there a solution to force my second call for $1 to... (4 Replies)
Discussion started by: Elmassimo
4 Replies

4. Shell Programming and Scripting

Reset while loop to loop same file multiple times

Hi, I want to read file multiple times. Right now i am using while loop but that is not working. ex. While read line do while read line2 do echo stmt1 #processing some data based on data., done < file2.txt done < file1.txt # This will have 10... (4 Replies)
Discussion started by: tmalik79
4 Replies

5. Shell Programming and Scripting

Automatic variable assignment inside a for loop

cs1=`echo "scale=8;($css1/$css0)*100"|bc` cs2=`echo "scale=8;($css2/$css0)*100"|bc` cs3=`echo "scale=8;($css3/$css0)*100"|bc` cs4=`echo "scale=8;($css4/$css0)*100"|bc` cs5=`echo "scale=8;($css5/$css0)*100"|bc` cs6=`echo "scale=8;($css6/$css0)*100"|bc` cs7=`echo "scale=8;($css7/$css0)*100"|bc`... (3 Replies)
Discussion started by: thulasidharan2k
3 Replies

6. Shell Programming and Scripting

How to give a variable output name in a shell script inside a for loop

Hi all I run my program prog.c in the following way : $ ./prog 1 > output.txt where 1 is a user defined initial value used by the program. But now I want to run it for many a thousand initial values, 1-1000, and store all the outputs in different files. Like $ ./prog 1... (1 Reply)
Discussion started by: alice06
1 Replies

7. Shell Programming and Scripting

BASH loop inside a loop question

Hi all Sorry for the basic question, but i am writing a shell script to get around a slightly flaky binary that ships with one of our servers. This particular utility randomly generates the correct information and could work first time or may work on the 12th or 100th attempt etc !.... (4 Replies)
Discussion started by: rethink
4 Replies

8. Shell Programming and Scripting

how to pass a variable to an update sql statement inside a loop

hi all, i am experiencing an error which i think an incorrect syntax for the where clause passing a variable was given. under is my code. sqlplus -s ${USERNAME}/${PASSWORD}@${SID} << END1 >> $LOGFILE whenever sqlerror exit set serveroutput on size 1000000 declare l_rc ... (0 Replies)
Discussion started by: ryukishin_17
0 Replies

9. Shell Programming and Scripting

Using variables created sequentially in a loop while still inside of the loop [bash]

I'm trying to understand if it's possible to create a set of variables that are numbered based on another variable (using eval) in a loop, and then call on it before the loop ends. As an example I've written a script called question (The fist command is to show what is the contents of the... (2 Replies)
Discussion started by: DeCoTwc
2 Replies

10. Shell Programming and Scripting

variable inside variable inside loop headache

Hi Gurus I have a file called /tmp/CMDB which looks like this serial: 0623AN1208 hostname: server1 model: x4100 assetID: 1234 I am writing a for loop that will go through this file line by line creating a variable of itself. Using the first iteration of the loop (i.e. the first line) as... (6 Replies)
Discussion started by: hcclnoodles
6 Replies
Login or Register to Ask a Question