awk获取外部变量

语法

awk [ -F re] [parameter...] ['pattern {action}' ] [-f progfile][in_file...]

  • 获得普通外部变量 
  • [xingxing.dxx@30_28_6_20 ~]$ test='test code'
    [xingxing.dxx@30_28_6_20 ~]$ echo | awk '{print test}' test="$test"
    test code 
  • BEGIN快中获取变量

  • [xingxing.dxx@30_28_6_20 ~]$ test='test code'
    [xingxing.dxx@30_28_6_20 ~]$ echo | awk -v test="$test" 'BEGIN{print test}'
    test code
    [xingxing.dxx@30_28_6_20 ~]$ echo | awk -v test="$test" '{print test}' 
    test code
  • 获取环境变量
  • [xingxing.dxx@30_28_6_20 ~]$ awk  'BEGIN{for (i in ENVIRON) {print i"="ENVIRON[i];}}'
    TERM=linux
    G_BROKEN_FILENAMES=1
    SHLVL=1
原文地址:https://www.cnblogs.com/duanxingxing/p/5167107.html