Assign AWK result to variable

This question already has an answer here:

  • How to set a variable to the output of a command in Bash? 13 answers

  • The following works correctly on bash:

     a=$(echo '111 222 33' | awk '{print $3;}' )
     echo $a # result is "33"
    

    Another option would be to convert the string to an array:

     a="111 222 333"
     b=($a)
    
     echo ${b[2]}  # returns 333
    

    或者你可以直接使用:

    ${INDEXCOUNT##* }
    

    这可能更容易用于测试if语句/

    INDEXCOUNT="111 222 333"
    echo $INDEXCOUNT | awk '{if ($3 == 333) print $3}';
    
    链接地址: http://www.djcxy.com/p/57198.html

    上一篇: 将值存储到Shell脚本中的变量

    下一篇: 将AWK结果分配给变量