Find the answer to your Linux question:
Results 1 to 10 of 10
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 ...
  1. #1
    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

  2. #2
    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

  3. #3
    scm
    scm is offline
    Linux Engineer
    Join Date
    Feb 2005
    Posts
    1,044
    The easiest way to fix the problem is to change your shebang line from /bin/bash to /bin/ksh - Korn does it "properly".

  4. #4
    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

  5. #5
    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

  6. #6
    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

  7. #7
    Linux User
    Join Date
    Jun 2007
    Posts
    318
    You forgot the eval.

    eval NEWPORT="\$LPORT$j"

  8. #8
    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

  9. #9
    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.

  10. #10
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...