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.
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
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
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
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.
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.
A Newbie's Getting Started Guide to Linux
Learn the basics of the Linux operating systems. Get to know what it is all about, and familiarize yourself with the practical side. Basically, if you're a complete Linux newbie and looking for a quick and easy guide to get you started this is it. 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