- 論壇徽章:
- 10
|
- $ awk 'BEGIN{while("ls -1" | getline)print ++i}'
- 1
- 2
- 3
- 4
- 5
- 6
復(fù)制代碼 想了下,管道就相當(dāng)指定了getline從某個地方去數(shù)據(jù),所以上面描述的現(xiàn)象都是正確的,
while("echo ok" | getline), echo只輸出了一行,所以getline只運行正常了一次,不是我之前理解的每次循環(huán)"echo ok" | getline 都執(zhí)行一次,而是getline每次都執(zhí)行,而echo ok只執(zhí)行一次。
就是不知道上面的time命令怎么沒跟ls一樣正確執(zhí)行了,這點求高手解釋。??
寫了這么多,耽誤大家時間了![]()
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. |
|