[SOLVED] A variable looses its value in a a while loop on Fedora 14
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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
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.
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.
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?"
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?"
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.