LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   A variable looses its value in a a while loop on Fedora 14 (https://www.linuxquestions.org/questions/linux-newbie-8/a-variable-looses-its-value-in-a-a-while-loop-on-fedora-14-a-4175498444/)

pollie2011 03-17-2014 02:17 AM

A variable looses its value in a a while loop on Fedora 14
 
Please help with a problem in a script file.

This is my script file. The value of S is lost after the first loop completed.
#!/bin/bash


REPORTFILE=/tmp/testreport.txt

V=`adder -V | awk '{print substr($1, 0, length($1)-1);}'`
mkdir -p reports/$V > /dev/null 2>&1
cat testall.conf | while read S
do
echo "Doing $S ..."
rm -f $REPORTFILE > /dev/null 2>&1
./test.sh $S > /dev/null 2>&1
if [ -s $REPORTFILE ]
then
mv $REPORTFILE reports/$V/$S.txt > /dev/null 2>&1
else
echo "$REPORTFILE for $S not found" > reports/$V/$S.txt
fi
done
echo
Only the first line in testall.conf is read the the script exit. After running the script the variable $S is empty.

evo2 03-17-2014 02:25 AM

Hi,

Quote:

Only the first line in testall.conf is read the the script exit
I'm not sure why only the first line of testall.conf is being read. It may be a good idea to reduce your script to the simplest example that exhibits the problem. Eg
Code:

#/bin/bash
cat testall.conf | while read S
do
  echo "The line is $S"
done

Quote:

After running the script the variable $S is empty.
This is the expected behaviour. The variable $S is local to that script and will _not_ be set in the shell that executed the script.

Evo2.

pollie2011 03-17-2014 03:45 AM

Variable looses it's value
 
Thank you, I did that and all the lines in the file testall.conf were displayed. When the test.sh script (within the testall script) $S is empty after the first file in testall.conf is processed and the script goes out of the loop

pollie2011 03-17-2014 03:46 AM

Quote:

Originally Posted by evo2 (Post 5135928)
Hi,


I'm not sure why only the first line of testall.conf is being read. It may be a good idea to reduce your script to the simplest example that exhibits the problem. Eg
Code:

#/bin/bash
cat testall.conf | while read S
do
  echo "The line is $S"
done


This is the expected behaviour. The variable $S is local to that script and will _not_ be set in the shell that executed the script.

Evo2.

How can I change the script to prevent this?

evo2 03-17-2014 03:49 AM

Hi,
Quote:

Originally Posted by pollie2011 (Post 5135956)
Thank you, I did that and all the lines in the file testall.conf were displayed.

Ok, great.

Quote:

When the test.sh script (within the testall script) $S is empty after the first file in testall.conf is processed and the script goes out of the loop
Sorry I don't understand. However, the approach to debugging is still the same. Reduce the script to the simplest example that shows the problem. Notice also that you are redirecting the output of most commands to /dev/null so you will not see any error messages.

Evo2.

evo2 03-17-2014 03:51 AM

Hi,
Quote:

Originally Posted by pollie2011 (Post 5135957)
How can I change the script to prevent this?

By sourcing instead of executing the script. However, are you should this is really what you want to do?

Evo2.

grail 03-17-2014 06:05 AM

I would add that the following two statements are not the same:
Quote:

The value of S is lost after the first loop completed.

After running the script the variable $S is empty
The first is due to the while loop being run inside a subshell due to the pipe. In this case the useless cat can be tossed as a simple redirect into the while loop will allow the file to be read:
Code:

while ...
do
  <do stuff here>
done<your_file

The second fails due to the script itself creating its own subshell when being run, hence all variables within it are lost once the script completes. evo2's last post would solve this, although my question then would be, "What is the point of having the 'S' variable available after the script completes?"

pollie2011 03-18-2014 07:18 AM

Quote:

Originally Posted by grail (Post 5136003)
I would add that the following two statements are not the same:

The first is due to the while loop being run inside a subshell due to the pipe. In this case the useless cat can be tossed as a simple redirect into the while loop will allow the file to be read:
Code:

while ...
do
  <do stuff here>
done<your_file

The second fails due to the script itself creating its own subshell when being run, hence all variables within it are lost once the script completes. evo2's last post would solve this, although my question then would be, "What is the point of having the 'S' variable available after the script completes?"

Thank you.

szboardstretcher 03-18-2014 07:19 AM

FWIW, i did have bad ram that caused something like the actual description here. "Variable losing a value after x."

pan64 03-18-2014 07:39 AM

One additional possible reason:
test.sh (or something inside it) accepts input from stdin, therefore it will/may slurp out all the content and the next read will only get EOF.
You can avoid happening this by:
Code:

cat file | while read S
do
....
  test.sh $S </dev/null >/dev/null 2>&1
....
done


cascade9 03-18-2014 07:59 AM

Fedora 14 is end of life, out of support since 2011!

Any reason why you havent installed a newer, supported version?


All times are GMT -5. The time now is 03:59 AM.