并且脚本运行完成

并且脚本运行完成

本文介绍了无法将Shell变量传递到AWK中,并且脚本运行完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将命令行参数传递到awk脚本中,但是这样做之后命令行变得无响应,我必须终止程序.

I am attempting to pass command line arguments into an awk script, but upon doing so the command line becomes unresponsive and I have to kill the program.

var1=$3
var2=$4
awk -v x="$var1" -f awk_script

我知道awk脚本可以看到该变量,因为它是由它打印的,但是它被挂起了,不允许我输入下一个命令.我看到过类似的帖子,说这是完成的方式,但是我知道问题出在这些行中,因为排除这三行可以使脚本运行完成.有什么想法吗?

I know the variable is seen by the awk script because it is printed by it, but it gets hung up and doesn't allow me to enter the next command. I have seen similar posts that say this is how it is done but I know the problem lies in these lines because excluding these three lines allows the script to run to completion. Any thoughts?

推荐答案

您的脚本未挂起-正在等待输入.

Your script does not hang - it's waiting for input.

要证明这一点,请在启动后在控制台中键入一些字符.

To prove this, type some characters into console after start.

您忘记提供要处理的文件的文件名.例如.如果要处理的文件名为FILE_TO_PROCESS:

You forgot to provide the file name of the file which shall be processed. E.g. if file to process is named FILE_TO_PROCESS:

var1=$3
var2=$4
awk -v x="$var1" -f awk_script FILE_TO_PROCESS

或者,您可以将内容通过管道传递到脚本的stdin中.例如:

Alternatetively, you may pipe contents into stdin of the script. E.g.:

var1=$3
var2=$4
awk -v x="$var1" -f awk_script <FILE_TO_PROCESS

或:

var1=$3
var2=$4
cat <FILE_TO_PROCESS | awk -v x="$var1" -f awk_script


通过在bash中进行测试,我做了一个有趣的(令我惊讶的)观察结果:


By testing this in my bash, I made an interesting (for me surprising) observation:

awk脚本任何模式都会立即终止(无需等待输入):

An awk script without any pattern terminates immediately (without waiting for input):

$ cat >no-scan.awk <<'EOF'
> BEGIN { print "x: "x }
> EOF

$ awk -v x="Hello" -f no-scan.awk
x: Hello

$

具有至少一个模式的awk脚本等待输入:

An awk script with at least one pattern waits for input:

$ cat >scan.awk <<'EOF'
> BEGIN { print "x: "x }
> $0 { print NR": "$0 }
> EOF

$ awk -v x="Hello" -f scan.awk
x: Hello
Some input typed with keyboard
1: Some input typed with keyboard
more
2: more
more
3: more

$

(我用 Ctrl + D 完成了输入.)

(I finished input with Ctrl+D.)

我不确定这是否是awk的默认行为.可能是,我应该提到我是在Windows 10的cygwin64中用gawk完成此操作的.

I'm not sure whether this is the default behavior of awk. May be, I should mention that I did this with gawk in cygwin64 of my Windows 10.

$ awk --version
GNU Awk 4.1.4, API: 1.1 (GNU MPFR 3.1.5-p2, GNU MP 6.1.2)
Copyright (C) 1989, 1991-2016 Free Software Foundation.

我试图找到有关此的一些参考,但是我找不到.摘录自 linux.die.net 上的手册:

I tried to find some reference about this but I couldn't. Excerpt from manual on linux.die.net:

...

最后,在所有输入都用完之后,gawk将执行 END块(如果有)中的代码.

Finally, after all the input is exhausted, gawk executes the code in the END block(s) (if any).

恕我直言,这并没有明确表示没有模式会跳过输入处理,尽管这对我来说很有意义.

IMHO, this does not say explicitly that the absence of patterns skips processing of input though it makes sense to me.

通过搜索该主题,我找到了其他示例.似乎即使是默认操作(无模式)也会强制处理输入以及getline的发生(即使它在 BEGIN 规则中).

By googling this topic I found other examples. It seems that even the default action (without pattern) forces processing of input as well as the occurrence of getline (even if it is in the BEGIN rule).

这篇关于无法将Shell变量传递到AWK中,并且脚本运行完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 03:26