awk调用shell

为什么会有这份记录:在帮同学传文件至服务器时,使用了scp,因此链接属性没有建立好,所以向通过awk完成。(更好的是通过tar传递)

附:awk中调用shell的方法。

参考:http://hi.baidu.com/leejun_2005/item/7e75be108091f2fd9d778a51

一。使用所以system()

awk程序中我们可以使用system() 函数去调用shell命令;如果system()括号里面的参数没有加上双引号的话,awk认为它是一个变量,它会从awk的变量里面把它们先置换为常量,然后再回传给shell;如果system()括号里面的参数有加上双引号的话,那么awk就直接把引号里面的内容回传给shell,作为shell的“命令行”。

ryoma@smartpc:~$ awk 'BEGIN{v1="echo";v2="abc";system(v1" "v2)}'
abc
ryoma@smartpc:~$


ryoma@smartpc:~$ awk 'BEGIN{v1="echo";v2="abc";system(v1 v2)}'
/bin/sh: echoabc: command not found
ryoma@smartpc:~$


ryoma@smartpc:~$ awk 'BEGIN{v1=echo;v2=abc;system(v1" "v2)}'
ryoma@smartpc:~$

二。使用print cmd | “/bin/bash”
ryoma@smartpc:~$ awk 'BEGIN{print "echo","abc"| "/bin/bash"}'
abc
ryoma@smartpc:~$


ryoma@smartpc:~$ awk 'BEGIN{print "echo","abc",";","echo","123"| "/bin/bash"}'
abc
123
ryoma@smartpc:~$

注意:无论使用system()还是print cmd | “/bin/bash”, awk都是新开一个shell,在相应的cmdline参数送回给shell,所以要注意当前shell变量与新开shell变量问题

 

posted on 2014-01-14 15:07  阿加  阅读(1454)  评论(0编辑  收藏  举报

导航