Bash scripting "while" problem

John Sellens jsellens-Iv5KO+h6AVB+Y12zHexnB0EOCMrvLtNR at public.gmane.org
Thu Jun 6 03:19:57 UTC 2013


| My wild guess without much
| comprehension is that the while (and thus the array) is in a subshell
| because of the pipe so the variable is never set in the script proper

I think that is correct.

When I have this problem, instead of piping into the while, I will
typically use a tmp file e.g.

    tfile="/tmp/${0}.$$"
    index=0
    ifconfig | awk '/inet addr:/ { print $2 }' > "$tfile"
    # input redirection at end of while
    while read line
    do
      tmp="${line}"
      tmp2="${tmp#addr:}"
      ip[${index}]="${tmp2%.?}"
      echo "ip[] is ${ip[${index}]}"
      index=$(($index+1))
    done < "$tfile"
    rm -f "$tfile"
    echo "outside while, first ip is ${ip[0]}"

(I didn't actually test that, but it should work I hope)

Hope that helps!

John
--
The Toronto Linux Users Group.      Meetings: http://gtalug.org/
TLUG requests: Linux topics, No HTML, wrap text below 80 columns
How to UNSUBSCRIBE: http://gtalug.org/wiki/Mailing_lists





More information about the Legacy mailing list