Bash变量在复合评估中不起作用

时间:2017-03-22 14:17:16

标签: bash

提前感谢您对此问题的帮助,这似乎很简单,但我无法修复。以下代码是自我解释的情况:

    #INPUTS
    PID=8255
    echo PID is $PID

    #WORKING REFERENCE (THIS WORSKS!)
    GPID=$(ps fxao pid,ppid,pgid,comm |awk '$1 == "8255"'| awk '{print $3}')
    echo GPID is $GPID

    #TAKE 1 (DOES NOT WORK)
    GPID1=$(ps fxao pid,ppid,pgid,comm |awk '$1 == "$PID"'| awk '{print $3}')
    echo GPID1 is $GPID1

    #TAKE 2 (DOES NOT WORK)
    GPID2=$(ps fxao pid,ppid,pgid,comm |awk '$1 == "$($PID)"'| awk '{print $3}')
    echo GPID2 is $GPID2

执行的输出:

PID is 8255
GPID is 8251
GPID1 is
GPID2 is

1 个答案:

答案 0 :(得分:0)

您必须根据文档页面为其指定一个值参数:http://www.gnu.org/software/gawk/manual/gawk.html#Using-Shell-Variables

PID=8255
echo PID is $PID
GPID1=$(ps fxao pid,ppid,pgid,comm | awk -v d1="$PID" '$1 == d1'| awk '{print $3}')
echo GPID1 is $GPID1