zoukankan      html  css  js  c++  java
  • awk如何区分shell脚本传进来的参数和自身的参数?awk如何获取shell脚本传进来的参数;awk中如何执行shell命令

      问题:对于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 "
    "}' | grep -v '^$' | awk -vuid=$1 '{cmd="curl  http://10.5.20.25:5000/intelligentPressure -d "uid="uid"&cpu_usr="$1"&cpu_sys="$2"&cpu_idl="$3"&cpu_wai="$4"&cpu_hiq="$5"&cpu_siq="$6"&mem_used="$7"&mem_buff="$8"&mem_cach="$9"&mem_free="$10"&load_1m="$11"&load_5m="$12"&load_15m="$13"&net_eth0_recv="$14"&net_eth0_send="$15"&io_read="$16"&io_writ="$17"&epoach="$18"&sys_int="$19"&sys_csw="$20"" -H  "Authorization: token you_mast_auth"";system(cmd)}'
    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

  • 相关阅读:
    新功能:查看TrackBack过来的文章
    新功能发布: 数据备份
    小题大作:.Text中恶意脚本过滤的新方法
    新增关闭邮件通知功能
    Will Mono Become the Preferred Platform for Linux Development
    Test Driven Development 资源
    收藏夹注意事项
    功能调整:阅读排行、回复排行
    首页增加了到第二书店的链接
    DotGNU Portable.NET
  • 原文地址:https://www.cnblogs.com/shengulong/p/9099443.html
Copyright © 2011-2022 走看看