分配系统命令的输出变量 [英] Assigning system command's output to variable

查看:117
本文介绍了分配系统命令的输出变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要运行的系统命令awk脚本并获得存储在变量输出。我一直在试图做到这一点,但是命令的输出总是转到壳,我​​无法捕捉到它。如何可以做到这一点任何想法?

I want to run the system command in an awk script and get its output stored in a variable. I've been trying to do this, but the command's output always goes to the shell and I'm not able to capture it. Any ideas on how this can be done?

例如:

$ date | awk --field-separator=! {$1 = system("strip $1"); /*more processing*/}

应该叫系统命令,而不是输出发送到外壳,应指定输出回 $ 1 更多的处理。 Rignt现在,它的输出发送到外壳和分配命令的RET code到 $ 1

Should call the strip system command and instead of sending the output to the shell, should assign the output back to $1 for more processing. Rignt now, it's sending output to shell and assigning the command's retcode to $1.

推荐答案

想通了。

我们使用awk的<一个href=\"http://www.gnu.org/software/gawk/manual/html_node/Two_002dway-I_002fO.html#Two_002dway-I_002fO\">Two-way I / O

{
  "strip $1" |& getline $1
}

把$ 1条和函数getline需要输出带回到$ 1

passes $1 to strip and the getline takes output from strip back to $1

这篇关于分配系统命令的输出变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆