1

Was hoping someone could advise on the following.

At present I have the following code and I was wanting to assign $3 and $5 to shell variables inside the {} brackets - is this possible:

while read line; do pntadm -P $line | awk '{if (( $2 == 00 && $1 != 00 ) || ( $2 == 04 )) print $3,$5}'; done < /tmp/subnet_list

To make things a little clearer:

root[my-box]# cat /tmp/final_list
111.222.333.0
root[my-box]# pntadm -P 111.222.333.0 | head

Client ID       Flags   Client IP       Server IP       Lease Expiration                Macro           Comment

00A0BCDE1FGHI1  00      111.222.333.001    111.222.333.253     04/06/2013                      macro1
00              04      111.222.333.002    111.222.333.253     Zero                            macro1
00              00      111.222.333.003    111.222.333.253     Zero                            macro1
00A0BCDE1FGHI2  00      111.222.333.004    111.222.333.253     05/06/2013                      macro1
root[my-box]#

The code I have a present should pick out three lines from above (IP's 111.222.333.001 ...002 and ...004).

Had I just needed the result of e.g. $5 I could do the following:

while read line; do date=$( pntadm -P $line | awk '{if (( $2 == 00 && $1 != 00 ) || ( $2 == 04 )) print $5}' ); done < /tmp/subnet_list

But I need both $3 and $5 together...

Without the use of an array, can anyone tell me how to assign $3 and $5 to shell variables, within the {} brackets - please?

Rgds

Cee

1

1 Answer 1

2

You cannot assign shell variables "within the {} braces" -- Anything you do to the environment within the awk process will vanish when the awk process exits. You need to print the data you require from awk and read it from the shell. Try this:

while read line; do 
    pntadm -P $line | 
    awk '($2 == "00" && $1 != "00") || ($2 == "04") {print $3,$5}' |
    while read client_ip lease_exp; do
        : do something with $client_ip and $lease_exp
    done 
done < /tmp/subnet_list

Or without awk

while read line; do 
    pntadm -P $line |
    while read clientID flags clientIP serverIP leaseExpiration macro comment; do
        if [[ ($flags == "00" && $clientID != "00") || ($flags == "04") ]]; then
            : do something with $clientIP and $leaseExpiration
        fi
    done 
done < /tmp/subnet_list
Sign up to request clarification or add additional context in comments.

1 Comment

Nice one! Exactly what I was looking for (a while within a while) :) Thanks Glenn

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.