一、得到普通外部变量post
[chengmo@localhost ~]$ test='awk code'
[chengmo@localhost ~]$ echo | awk '{print test}' test="$test"
awk code
[chengmo@localhost ~]$ echo | awk test="$test" '{print test}'
awk: cmd. line:1: fatal: cannot open file `{print test}' for reading (No such file or directory)
格式如:awk ‘{action}’ 变量名=变量值 ,这样传入变量,能够在action中得到值。 注意:变量名与值放到’{action}’后面。
[chengmo@localhost ~]$ echo | awk 'BEGIN{print test}' test="$test"
这种变量在:BEGIN的action不能得到。
2.BEGIN程序块中变量
[chengmo@localhost ~]$ test='awk code'
[chengmo@localhost ~]$ echo | awk -v test="$test" 'BEGIN{print test}'
awk code
[chengmo@localhost ~]$ echo | awk -v test="$test" '{print test}'
awk code
格式如:awk –v 变量名=变量值 [–v 变量2=值2 …] 'BEGIN{action}’ 注意:用-v 传入变量能够在3中类型的action 中均可以得到到,但顺序在 action前面。
3.得到环境变量
[chengmo@localhost ~]$ awk 'BEGIN{for (i in ENVIRON) {print i"="ENVIRON[i];}}'
AWKPATH=.:/usr/share/awk
SSH_ASKPASS=/usr/libexec/openssh/gnome-ssh-askpass
SELINUX_LEVEL_REQUESTED=
SELINUX_ROLE_REQUESTED=
LANG=en_US.UTF-8
.......
只须要调用:awk内置变量 ENVIRON,就能够直接得到环境变量。它是一个字典数组。环境变量名 就是它的键值。