Skip to content

20.28 如何向awk脚本中传递外部变量

https://scz.617.cn/unix/201509231030.txt

A: scz 2015-09-23 10:30

有两种办法,一种是单引号,另一种是-v。


!/bin/bash

nawk="/bin/nawk" CURRENT_SHELL_PID=$(echo $$)

与awk脚本的输出形成对比

printf "%u\n" "${CURRENT_SHELL_PID}"

nawkcmd=' BEGIN \ { # # 为了引用外部变量,必须使用单引号,否则不会发生替换 # current_shell_pid = "'${CURRENT_SHELL_PID}'" printf( "%u\n", current_shell_pid ) } '

${nawk} "${nawkcmd}"

!/bin/bash

nawk="/bin/nawk" CURRENT_SHELL_PID=$(echo $$)

与awk脚本的输出形成对比

printf "%u\n" "${CURRENT_SHELL_PID}"

nawkcmd=' BEGIN \ { printf( "%u\n", current_shell_pid ) } '

可以指定多个-v

${nawk} -v current_shell_pid="${CURRENT_SHELL_PID}" "${nawkcmd}"