Welcome to Linux Forums! With a comprehensive Linux Forum, information on various types of Linux software and many Linux Reviews articles, we have all the knowledge you need a click away, or accessible via our knowledgeable members.
Find the answer to your Linux question:
New to Linux Forums? Register here for free!
    Linux Forums > GNU Linux Zone > Linux Programming & Scripting > I loose the variable value when I exit the while loop

Forgot Password?
 Linux Programming & Scripting   C, Perl, PHP, Bash Scripts, anything programming or script related post in here!

Site Navigation
Linux Articles
Linux Forums
Linux Downloads
Linux Hosting
Free Magazines
Job Board
IRC Chat
RSS Feeds


Linux Forum Topics
Linux Forums
Your Distro
Linux Resources
GNU Linux Zone
The Community
Reply
 
Thread Tools Display Modes
Old 05-08-2008   #1 (permalink)
Just Joined!
 
Join Date: Mar 2008
Posts: 30
I loose the variable value when I exit the while loop

Hi,

j=0
grep "^Listen" httpd.conf | awk '{print $2}'|
while read var
do
eval "LPORT"$j=$var
eval echo "\$LPORT$j"
j=$[j+1]
done
echo $LPORT1

The echo inside the while works and prints the variable but the last echo of $LPORT1 (which insile while had a valid value) is empty.

Thanks,
Bianca
potro is offline  


Reply With Quote
Old 05-08-2008   #2 (permalink)
Linux User
 
Join Date: Jun 2007
Posts: 318
That's because piping the output to a while loop causes a child process to be spawned. When the loop finishes the child process is deleted thus losing the value. The only solution I found is to redirect the output to a file and do this:

Code:
j=0
grep "^Listen" httpd.conf | awk '{print $2}' > tmp.tmp
while read var
do
eval "LPORT"$j=$var
eval echo "\$LPORT$j"
j=$[j+1]
done < tmp.tmp
echo $LPORT1
vsemaska is offline   Reply With Quote
Old 05-08-2008   #3 (permalink)
scm
Linux Engineer
 
Join Date: Feb 2005
Posts: 1,004
The easiest way to fix the problem is to change your shebang line from /bin/bash to /bin/ksh - Korn does it "properly".
scm is offline   Reply With Quote
Old 05-09-2008   #4 (permalink)
Just Joined!
 
Join Date: Mar 2008
Posts: 30
Quote:
Originally Posted by vsemaska View Post
That's because piping the output to a while loop causes a child process to be spawned. When the loop finishes the child process is deleted thus losing the value. The only solution I found is to redirect the output to a file and do this:

Code:
j=0
grep "^Listen" httpd.conf | awk '{print $2}' > tmp.tmp
while read var
do
eval "LPORT"$j=$var
eval echo "\$LPORT$j"
j=$[j+1]
done < tmp.tmp
echo $LPORT1
Is then another solution to the problem: I grep a file and put the values in variables:
E.g.:
[root@skynet]# grep "^Listen" httpd.conf | awk '{print $2}'
FrontEnd_1_IP:8081
FrontEnd_2_IP:8081
8081
8082
8083

I need to create variables:
$LPORT1 with value FrontEnd_1_IP:8081
$LPORT2 with value FrontEnd_1_IP:8081
etc.

and I need to use this variables inside my script in other functions etc.

Your solution works ... but if the I enter another while or another function inside my script and I want to use the variables they don't have a value

Thanks a lot,
Bianca
potro is offline   Reply With Quote
Old 05-10-2008   #5 (permalink)
Linux User
 
Join Date: Jun 2007
Posts: 318
The way your loop is set up your variables will start with LPORT0 not LPORT1. You have to move your 'j=$[j+1]' line before the 1st eval line to start with LPORT1.

Can you give an example of a script where it's not working? I tried it and the values are there within another loop.

Code:
#!/bin/bash -vx

j=0
grep "^Listen" httpd.conf | awk '{print $2}' > tmp.tmp
while read var
do
j=$[j+1]
eval "LPORT"$j=$var
eval echo "\$LPORT$j"
done < tmp.tmp
echo $LPORT1

j=0
while read
   do
        j=$[j+1]
        eval echo "\$LPORT$j"
   done < tmp.tmp
vsemaska is offline   Reply With Quote
Old 05-12-2008   #6 (permalink)
Just Joined!
 
Join Date: Mar 2008
Posts: 30
Quote:
Originally Posted by vsemaska View Post
The way your loop is set up your variables will start with LPORT0 not LPORT1. You have to move your 'j=$[j+1]' line before the 1st eval line to start with LPORT1.

Can you give an example of a script where it's not working? I tried it and the values are there within another loop.

Code:
#!/bin/bash -vx

j=0
grep "^Listen" httpd.conf | awk '{print $2}' > tmp.tmp
while read var
do
j=$[j+1]
eval "LPORT"$j=$var
eval echo "\$LPORT$j"
done < tmp.tmp
echo $LPORT1

j=0
while read
   do
        j=$[j+1]
        eval echo "\$LPORT$j"
   done < tmp.tmp

Hi

Indeed it works. I can't understand what I tested.

The issue I still have is to give this values to another variable:
j=0
while read
do
j=$[j+1]
NEWPORT="\$LPORT$j"
echo $NEWPORT
# I do something with the variable
done

NEWPORT variable has the values: $LPORT1, $LPORT2 and not the value of the variables.

Thanks,
Bianca
potro is offline   Reply With Quote
Old 05-12-2008   #7 (permalink)
Linux User
 
Join Date: Jun 2007
Posts: 318
You forgot the eval.

eval NEWPORT="\$LPORT$j"
vsemaska is offline   Reply With Quote
Old 05-12-2008   #8 (permalink)
Just Joined!
 
Join Date: Mar 2008
Posts: 30
Quote:
Originally Posted by vsemaska View Post
You forgot the eval.

eval NEWPORT="\$LPORT$j"
Indeed.


Thanks a lot,
Bianca
potro is offline   Reply With Quote
Old 05-12-2008   #9 (permalink)
Linux User
 
Join Date: Jun 2007
Posts: 318
It's because you directed the shell to get standard input (channel 0) from tmp.tmp with the command:

done < tmp.tmp

If you want to prompt for input from standard input then you have to read from tmp.tmp on a different channel. In this example I'll use channel 3.

Code:
j=0
i=0
exec 3<tmp.tmp # Open tmp.tmp for reading on channel 3
while read -u3 # Read from channel 3
do
j=$[j+1]
eval NEWLPORT="\$LPORT$j"
echo $ANEWLPORT
echo "Please type the Listen Port and F when finish : (current value:$NEWLPORT )"
read -e NEWLPORT
if [ "$NEWLPORT" != "F" ]
then
i=$[i+1]
eval "LISTEN_PORT"$i=$NEWLPORT
fi
done
exec 3<&- # Close channel 3.
vsemaska is offline   Reply With Quote
Old 05-14-2008   #10 (permalink)
Just Joined!
 
Join Date: Mar 2008
Posts: 30
Thanks for your time.
With your help I managed to solve my script issue.

Have a nice day,
Bianca
potro is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Free Magazines
Run Your Own Web Server Using Linux & Apache - Free 191 Page Preview
Learn about everything you'll need to build and maintain your Linux servers, and to deploy Web applications to them.
subscribe
Open Source Security Myths Dispelled
Dispel the five major myths surrounding Open Source Security and gain the tools necessary to make a truly informed decision for your IT organization
subscribe
InformationWeek
InformationWeek is the only newsweekly you'll need to stay on top of the latest developments in information technology.
subscribe



All times are GMT. The time now is 06:22 AM.






© 2000 - 2009 - All Rights Reserved - Property of  MAS Media

Content Relevant URLs by vBSEO 3.3.0 RC2