求解释awk getline的用法
时间:2011-06-20
来源:互联网
本帖最后由 liion631818 于 2011-06-20 16:51 编辑
复制代码
为什么结果不是
复制代码
可以肯定 "echo ok" | getline 只执行了一次,可以通过下面的代码证明:
复制代码
why???
如果管道前面命令输出有多行,getline就执行失败了,下面的代码变量 i 没有被打印出来
复制代码
why???
例外,shell中管道2边会fork出新的进程,在awk中怎样理解?
- cat file
- 1
- 2
- 3
- 4
- awk '{"echo ok" | getline; print}' file
- ok
- 2
- 3
- 4
- ok
- ok
- ok
- ok
- awk 'BEGIN{while("echo ok" | getline) print ++i}'
- 1
如果管道前面命令输出有多行,getline就执行失败了,下面的代码变量 i 没有被打印出来
- awk 'BEGIN{while("time" | getline)print ++i}'
- real 0m0.000s
- user 0m0.000s
- sys 0m0.000s
例外,shell中管道2边会fork出新的进程,在awk中怎样理解?
作者: liion631818 发布时间: 2011-06-20
- $ awk 'BEGIN{while("ls -1" | getline)print ++i}'
- 1
- 2
- 3
- 4
- 5
- 6
while("echo ok" | getline), echo只输出了一行,所以getline只运行正常了一次,不是我之前理解的每次循环"echo ok" | getline 都执行一次,而是getline每次都执行,而echo ok只执行一次。
就是不知道上面的time命令怎么没跟ls一样正确执行了,这点求高手解释。??
写了这么多,耽误大家时间了

QUOTE:
When the output of a command is piped to getline and it contains multiple lines, getline reads a
line at a time. The first time getline is called it reads the first line of output. If you call it again, it
reads the second line. To read all the lines of output, you must set up a loop that executes getline
until there is no more output. For instance, the following example uses a while loop to read each line
of output and assign it to the next element of the array, who_out:
while ("who" | getline)
who_out[++i] = $0
Each time the getline function is called, it reads the next line of output. The who command,
however, is executed only once.
line at a time. The first time getline is called it reads the first line of output. If you call it again, it
reads the second line. To read all the lines of output, you must set up a loop that executes getline
until there is no more output. For instance, the following example uses a while loop to read each line
of output and assign it to the next element of the array, who_out:
while ("who" | getline)
who_out[++i] = $0
Each time the getline function is called, it reads the next line of output. The who command,
however, is executed only once.
作者: liion631818 发布时间: 2011-06-20
本帖最后由 lionfun 于 2011-06-20 18:47 编辑
回复 liion631818
我觉得与time的这个命令的特殊性有关!题目中的time命令应该是bash的builtin命令!
time的输出是stderr,其直接在当前主shell中执行,这个time命令特殊的地方就是对命令的重定向无法在当前shell环境完成
将这个time命令放入subshell执行:
复制代码
如果是非bash的builtin命令,即/usr/bin/time,就可以重定向:
复制代码
回复 liion631818
我觉得与time的这个命令的特殊性有关!题目中的time命令应该是bash的builtin命令!
time的输出是stderr,其直接在当前主shell中执行,这个time命令特殊的地方就是对命令的重定向无法在当前shell环境完成
将这个time命令放入subshell执行:
- [root@station3 ~]# awk 'BEGIN{while("(time;)2>&1" | getline)print ++i}'
- 1
- 2
- 3
- 4
- [root@station3 ~]# /usr/bin/time -p pwd 2>&1 1>/dev/null
- real 0.00
- user 0.00
- sys 0.00
- [root@station3 ~]# awk 'BEGIN{while("/usr/bin/time -p pwd 2>&1 1>/dev/null" | getline)print ++i}'
- 1
- 2
- 3
作者: lionfun 发布时间: 2011-06-20
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28