AWK - 与函数getline变量的传输系统()? [英] AWK - Transmission of a variable with getline to system ()?

查看:146
本文介绍了AWK - 与函数getline变量的传输系统()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个理论问题:

1)如何把一个变量传递给函数getline的系统()?

 的awk'BEGIN {VAR =LS; VAR |函数getline VAR;系统(回声$ VAR)}'

2)如何分配变量输出系统(LS),并在AWK打印出结果?

 的awk'BEGIN {VAR =系统(LS);打印是$ var'}'

3)您可以分配系统(一个变量var =LS),并在AWK打印出结果?

 的awk'BEGIN {系统(VAR =LS);打印'是$ var'}'

感谢您的信息。

编辑:

托雷克:谢谢您的答复。

据我所知,在第一个例子,你可以这样做:

 的awk'BEGIN {而(ls -l命令|函数getline VAR)系统(回声VAR);}

对于这个应用程序,你不能分配从系统的可变输出()?如在本实施例

 的awk'BEGIN {VAR =ls -l命令;系统(VAR);打印VAR}


解决方案

您正在寻找这个错误的方式,我想。 awk的系统只是需要任何旧的字符串,所以给它一次,例如:

 系统(回声VAR); #见下文侧面说明

(记住,在awk中,字符串由相邻级联)。此外,系统刚刚运行的命令;捕获它的输出,你需要使用函数getline ,类似于你的问题#1。

如果你想读 LS的所有输出您需要循环的结果从函数getline

 的awk'BEGIN {而(LS|函数getline VAR)打印我:VAR; }

由于这仅仅定义了一个 BEGIN 操作, AWK 将启动,运行 LS ,收集每个输出线和打印出来,然后退出。


旁注:非常小心传递给一个shell变量(包括在的左侧两个呼叫系统和物品|函数getline ,加上其他一些情况下,在现代品种 AWK -anything的运行命令)。反引号, $(命令)和分号都可以让用户调用任意命令。例如,在系统(回声VAR)上面的例子中,如果 VAR 包含 ;室射频$ HOME 命令变成回声;室射频$ HOME ,这是几乎可以肯定不是你想有发生什么。

您可以检查坏字,要么对象或引用他们。现代8位干净的壳应该只需要引用引号本身(句法有效性), $ < > | `。如果使用单引号引用参数(以使它们看上去像一个字),你只需要逃避单引号。请参见这unix.stackexchange.com回答了解更多详情。


另外一个侧面说明:我倾向于不必要的分号添加到我的awk脚本,使它们看起来更象C语法。旧习惯,从几十年前。

I have a theoretical question:

1) How pass a variable to the system of getline ()?

awk 'BEGIN{var="ls"; var | getline var; system("echo $var")}'

2) How to assign a variable the output system ("ls") and print the result in awk?

awk 'BEGIN{var="system("ls")"; print '$var'}'

3) Can you assign a variable in the system (var = "ls") and print the result in awk?

awk 'BEGIN{system(var="ls"); print "'"$var"'"}'

Thank you for the information.

EDIT:

torek: Thank you for your response.

I understand that in the first example, you can do this:

awk 'BEGIN { while ("ls -l" | getline var) system("echo " var  );}'

For this application, you can not assign a variable output from system ()? As in this example:

awk 'BEGIN {var="ls -l"; system(var); print var}'

解决方案

You're looking at this the wrong way, I think. Awk's system just takes any old string, so give it one, e.g.:

system("echo " var);  # see side note below

(remember that in awk, strings are concatenated by adjacency). Moreover, system just runs a command; to capture its output, you need to use getline, similar to your question #1.

If you want to read all the output of ls you need to loop over the result from getline:

awk 'BEGIN { while ("ls" | getline var) print "I got: " var; }'

Since this defines only a BEGIN action, awk will start up, run ls, collect each output line and print it, and then exit.


Side note: be very careful with variables passed to a shell (this includes both calls to system and items on the left hand side of | getline, plus some other cases in modern varieties of awk—anything that runs a command). Backquotes, $(command), and semicolons can all allow users to invoke arbitrary commands. For instance, in the system("echo " var) example above, if var contains ; rm -rf $HOME the command becomes echo ; rm -rf $HOME, which is almost certainly not something you want to have happen.

You can check for "bad" characters and either object, or quote them. Modern 8-bit-clean shells should only require quoting quotes themselves (for syntactic validity), $, <, >, |, and `. If you use single quotes to quote arguments (to make them appear as a single "word"), you need only escape the single quotes. See this unix.stackexchange.com answer for more details.


One other side note: I tend to add "unnecessary" semicolons to my awk scripts, making them look more like C syntactically. Old habit from decades ago.

这篇关于AWK - 与函数getline变量的传输系统()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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