3

我有一个问题,我需要调用传入参数的 Perl 脚本并在 AWKBEGIN块中获取 Perl 脚本的返回值。就像下面一样。

我有一个 Perl 脚本 util.pl

#!/usr/bin/perl -w
$res=`$exe_cmd`;
print $res;

现在在 AWKBEGIN块 (ksh) 中,我需要调用脚本并获取返回值。

BEGIN { print "in awk, application type is " type;  
                    } \
            {call per script here;}

如何使用参数调用 Perl 脚本并获取返回值$res

res = util.pl a b c; 
4

2 回答 2

2

Pipe the script into getline:

awk 'BEGIN {
         cmd = "util.pl a b c"; 
         cmd | getline res; 
         close(cmd);
         print "in awk, application type is " res
     }'
于 2012-05-24T10:58:45.307 回答
0

Part of an AWK-script I use for extracting data from a ldap query. Perhaps you can find some inspiration from how I do the base64 decoding below...

    /^dn:/{
        if($0 ~ /^dn: /){
            split($0, a, "[:=,]")
            name=a[3]
        }
        else if($0 ~ /^dn::/){
            # Special handling needed since ldap apparently
            # uses base64 encoded strings for *some* users
            cmd = "/usr/bin/base64 -i -d <<< " $2 " 2>/dev/null"
            while ( ( cmd | getline result ) > 0 ) { }
            close(cmd)
            split(result, a, "[:=,]")
            name=a[2]
        }
    }
于 2012-05-24T10:58:34.987 回答