zoukankan      html  css  js  c++  java
  • awk中执行Linux命令的两种方式

    在使用awk处理内容时,有时会按行执行Linux命令,下面介绍两种执行Linux命令方式。

    方式一

    用system():

    [root@localhost shell_script]# awk 'BEGIN {system("pwd")}'
    /root/shell_script
    [root@localhost shell_script]#
    

    test.log文件:

    [root@localhost shell_script]# cat test.log 
    pwd
    ls
    which sh
    [root@localhost shell_script]#
    

    按文件行作为输入执行:

    $0 表示一行数据

    [root@localhost shell_script]# awk '{print "执行命令:"$0; system($0); print "
    "}' test.log
    执行命令:pwd
    /root/shell_script
    
    执行命令:ls
    awk_system.sh  test.log
    
    执行命令:which sh
    /usr/bin/sh
    
    [root@localhost shell_script]#
    

    拼接命令:

    [root@localhost shell_script]# echo "sh601236" | awk '{cmd = "curl -X GET http://hq.sinajs.cn/list="$0; system(cmd);}'
    var hq_str_sh601236="红塔证券,19.930,20.320,19.150,19.930,18.710,19.150,19.160,41190044,791608359.000,198000,19.150,53300,19.140,60100,19.130,74200,19.120,24100,19.110,50500,19.160,28500,19.170,18300,19.180,15500,19.190,59300,19.200,2020-06-29,15:00:00,00,";
    [root@localhost shell_script]#
    

    方式二

    借助|sh命令:
    [root@localhost shell_script]# echo "pwd" | sh
    /root/shell_script
    [root@localhost shell_script]# cat test.log | sh
    /root/shell_script
    awk_system.sh  test.log
    /usr/bin/sh
    [root@localhost shell_script]#
    

    print

    [root@localhost shell_script]# awk 'BEGIN {print "pwd" | "sh"}'
    /root/shell_script
    [root@localhost shell_script]#
    
    [root@localhost shell_script]# echo "pwd" | awk '{print $0 | "sh"}'
    /root/shell_script
    [root@localhost shell_script]#
    
    [root@localhost shell_script]# awk '{print "执行命令:"$0; print $0 | "sh"; print "
    "}' test.log
    执行命令:pwd
    
    执行命令:ls
    
    执行命令:which sh
    
    /root/shell_script
    awk_system.sh  test.log
    /usr/bin/sh
    [root@localhost shell_script]#
    
    原创 Doflamingo https://www.cnblogs.com/doflamingo
  • 相关阅读:
    002. 在HTML页面嵌入循环代码
    001. 为input type=text 时设置默认值
    PHP包名解释
    003. vs2010发布、打包安装程序(转)
    SQL server 2008 Express Edition实现自动备份和自动删除备份
    解决phpMyAdmin中缺少mysqli扩展的错误
    IIS6下PHP环境的资源未找到(404)问题
    解决远程桌面连接过去后是蓝色屏幕问题
    解决tomcat一闪而过问题
    解决访问远程共享时发生 请检查名称的拼写. 否则, 网络可能有问题 故障
  • 原文地址:https://www.cnblogs.com/doflamingo/p/13209610.html
Copyright © 2011-2022 走看看