LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 03-17-2014, 03:17 AM   #1
pollie2011
LQ Newbie
 
Registered: Feb 2011
Posts: 5

Rep: Reputation: Disabled
Angry 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.
 
Old 03-17-2014, 03:25 AM   #2
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and Scientific Linux
Posts: 5,753

Rep: Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288
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.
 
Old 03-17-2014, 04:45 AM   #3
pollie2011
LQ Newbie
 
Registered: Feb 2011
Posts: 5

Original Poster
Rep: Reputation: Disabled
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
 
Old 03-17-2014, 04:46 AM   #4
pollie2011
LQ Newbie
 
Registered: Feb 2011
Posts: 5

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by evo2 View Post
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?
 
Old 03-17-2014, 04:49 AM   #5
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and Scientific Linux
Posts: 5,753

Rep: Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288
Hi,
Quote:
Originally Posted by pollie2011 View Post
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.
 
Old 03-17-2014, 04:51 AM   #6
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and Scientific Linux
Posts: 5,753

Rep: Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288
Hi,
Quote:
Originally Posted by pollie2011 View Post
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.
 
Old 03-17-2014, 07:05 AM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,252

Rep: Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685
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?"
 
Old 03-18-2014, 08:18 AM   #8
pollie2011
LQ Newbie
 
Registered: Feb 2011
Posts: 5

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by grail View Post
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.
 
Old 03-18-2014, 08:19 AM   #9
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 3,782
Blog Entries: 1

Rep: Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339
FWIW, i did have bad ram that caused something like the actual description here. "Variable losing a value after x."
 
Old 03-18-2014, 08:39 AM   #10
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,110

Rep: Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268
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
 
Old 03-18-2014, 08:59 AM   #11
cascade9
Senior Member
 
Registered: Mar 2011
Location: Brisneyland
Distribution: Debian, aptosid
Posts: 3,718

Rep: Reputation: 906Reputation: 906Reputation: 906Reputation: 906Reputation: 906Reputation: 906Reputation: 906Reputation: 906
Fedora 14 is end of life, out of support since 2011!

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


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] output to a variable in while loop Leath Linux - Newbie 10 07-29-2012 11:32 AM
variable incrementation in for loop ++nick++ Linux - General 3 10-20-2011 03:03 PM
[SOLVED] variable is gone at the end of while loop ted_chou12 Programming 9 03-28-2011 04:57 AM
[SOLVED] [BASH] non-empty variable before loop end, is empty after exiting loop aitor Programming 2 08-26-2010 10:57 AM
getting a variable out of a WHILE loop. fhinkle Programming 8 02-22-2005 05:43 AM


All times are GMT -5. The time now is 02:46 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration