vinllen
vinllen

Reputation: 1459

How to pass variables from awk to shell

How to pass variables from awk to shell, e.g.

#!/bin/sh

cat in_arp | awk 'BEGIN{FS = "[: ]+"; arp_nr = 0} {
    if(NR == 2) {
        printf("%s\n", $3)
        arp_nr = $3 #get this variable
    }
    else if(NR >= 5)
        printf("%s\n", $1)
}'

cat in_route | awk 'BEGIN{FS = "[: ]+"; route_nr = 0} {
    if($1 == "Total")
        printf("%s\n", $4)
    else if(NR >= 3) {
        printf("%s\n", $1)
        route_nr = $1 #get this variable
    }
}' | tac

echo $arp_nr, $route_nr # what i want are these two variables

  I want to compare arp_nr and route_nr, but how to get these two variables from awk to shell so i can compare them. In the above code, echo $arp_nr, $route_nr returns none

Upvotes: 2

Views: 4360

Answers (4)

Victor Choy
Victor Choy

Reputation: 4246

A better solution for me

eval $(awk 'BEGIN{ print "vech=Bus"}' < /dev/null)

You can try like this

reuslt=`awk '{...} END{print "aa="aa}'`
eval $result

Upvotes: 0

bstipe
bstipe

Reputation: 282

Change the two awk programs into one may be an option. It is hard to tell from the information presented. Here is a guide.

#!/bin/sh

awk 'BEGIN{FS = "[: ]+"; arp_nr = 0; route_nr = 0} {
    if( FILENAME == "in_arp" ) {
        if(NR == 2) {
            printf("%s\n", $3)
            arp_nr = $3 #get this variable
        }
        else if(NR >= 5)
            printf("%s\n", $1)
    }

#   This code would need to be changed to mimic the 'tac' command
    if( FILENAME == "in_route" ) {
        if($1 == "Total")
            printf("%s\n", $4)
        else if(NR >= 3) {
            printf("%s\n", $1)
            route_nr = $1 #get this variable
        }
    }

} END{ if(arp_nr == route_nr) { print "Something!!" }
}' in_arp in_route 

Upvotes: 0

λuser
λuser

Reputation: 1001

AWK variables are not shell variables. Even if you made the variables environment variables, AWK would set them for AWK child processes, not for its parent process (your script).

A way would be to write the AWK variables to a file and read the file in your shell. With AWK, you could do print arp_nr > "arp_nr.var", and after running awk, in your script do arp_nr=$(cat arp_nr.var).

You could also output all vars in the same file with printf("arp_nr=%s", arp_nr) >> "my_vars.sh" (same for route_nr) and then simply run source my_vars.sh in your shell. However, in this case, since you run source, it might be a security risk if an attacker could create the file my_vars.sh, because then you would run everything the attacker put in it. Consider using mktemp instead of a hardcoded filename. mktemp ensures that nobody created the file before you.

Upvotes: 4

I think you cannot retrieve variables from awk to shell directly.

You have to print them and get it in an other variable, such as:

var=$(cat in_arp | awk 'BEGIN{FS = "[: ]+"; arp_nr = 0} {
    if(NR == 2) {
        printf("%s\n", $3)
        arp_nr = $3 #get this variable
        printf("TEST_%s_TEST", $arp_nr)
    }
    else if(NR >= 5)
        printf("%s\n", $1)
}')

And split $var to get TEST_$arp_nr_TEST. With sed for example:

echo $var | sed s/.*TEST_\(.*\)_TEST.*/\1/

Upvotes: 0

Related Questions