问题:对于shell脚本,$0表示脚本本身,$1表示脚本的第一个参数,$2……依次类推;对于awk,$1表示分割后的第一个字段,$2……依次类推。那么对于shell脚本中的awk如何区分两者呢?

  答案:通过awk的变量定义,把shell脚本的参数值赋值给awk的自定义变量,然后通过变量引用,使用shell传进来的参数

  举例:下面的脚本test.sh内容如下,带参数执行脚本:sh test.sh test,其中uid的值就是参数test   

1 step=2 #间隔的秒数,不能大于60
2 for (( i = 0; i < 60; i=(i+step) ))
3 do
4     dstat -c -m -l -n -N eth0 -r -T -y 1 1 | sed -n '4,$p' | awk -F'[ \\|]' '{for(i=1;i<=NF;i++)printf $i" "; print "\n"}' | grep -v '^$' | awk -vuid=$1 '{cmd="curl  http://10.5.20.25:5000/intelligentPressure -d \"u>'
5     sleep $step
6 done
7 exit 0

 如果多个变量,就定义多个:awk -v a=$1 -v b=$2

参考:

1、http://www.runoob.com/linux/linux-comm-awk.html

2、https://blog.csdn.net/cy_cai/article/details/41908921

相关文章: