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变量问题

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值