LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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, 02: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, 02:25 AM   #2
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,724

Rep: Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705
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, 03: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, 03: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, 03:49 AM   #5
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,724

Rep: Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705
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, 03:51 AM   #6
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,724

Rep: Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705
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, 06:05 AM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
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, 07: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, 07:19 AM   #9
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278

Rep: Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694
FWIW, i did have bad ram that caused something like the actual description here. "Variable losing a value after x."
 
Old 03-18-2014, 07:39 AM   #10
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,930

Rep: Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321
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, 07:59 AM   #11
cascade9
Senior Member
 
Registered: Mar 2011
Location: Brisneyland
Distribution: Debian, aptosid
Posts: 3,753

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

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


Reply



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 10:32 AM
variable incrementation in for loop ++nick++ Linux - General 3 10-20-2011 02:03 PM
[SOLVED] variable is gone at the end of while loop ted_chou12 Programming 9 03-28-2011 03:57 AM
[SOLVED] [BASH] non-empty variable before loop end, is empty after exiting loop aitor Programming 2 08-26-2010 09:57 AM
getting a variable out of a WHILE loop. fhinkle Programming 8 02-22-2005 04:43 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:42 AM.

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
Open Source Consulting | Domain Registration