如何通过shebang(即#!)使用awk的多个参数? [英] How to use multiple arguments for awk with a shebang (i.e. #!)?
问题描述
我想使用 --re 执行 gawk 脚本-interval
使用shebang.
I'd like to execute an gawk script with --re-interval
using a shebang. The "naive" approach of
#!/usr/bin/gawk --re-interval -f
... awk script goes here
不起作用,因为 gawk 是用第一个参数 "--re-interval -f"
调用的(没有围绕空格分割),它不理解.有解决方法吗?
does not work, since gawk is called with the first argument "--re-interval -f"
(not splitted around the whitespace), which it does not understand. Is there a workaround for that?
当然,您可以不直接调用 gawk 而是将其包装到拆分第一个参数的 shell 脚本中,或者创建一个 shell 脚本然后调用 gawk 并将脚本放入另一个文件中,但我想知道是否有一些在一个文件中执行此操作的方法.
Of course you can either not call gawk directly but wrap it into a shell script that splits the first argument, or make a shell script that then calls gawk and put the script into another file, but I was wondering if there was some way to do this within one file.
shebang 行的行为因系统而异 - 至少在 Cygwin 中没有用空格分割参数.我只关心如何在表现如此的系统上做到这一点;该脚本并不意味着可移植.
The behaviour of shebang lines differs from system to system - at least in Cygwin it does not split the arguments by whitespaces. I just care about how to do it on a system that behaves like that; the script is not meant to be portable.
推荐答案
这对我来说似乎适用于 (g)awk.
This seems to work for me with (g)awk.
#!/bin/sh
arbitrary_long_name==0 "exec" "/usr/bin/gawk" "--re-interval" "-f" "$0" "$@"
# The real awk program starts here
{ print $0 }
注意#!
运行/bin/sh
,所以这个脚本首先被解释为一个shell脚本.
Note the #!
runs /bin/sh
, so this script is first interpreted as a shell script.
起初,我只是尝试了 "exec" "/usr/bin/gawk" "--re-interval" "-f" "$0" "$@"
,但是 awk 处理了那个作为命令并无条件地打印出每一行输入.这就是为什么我输入 arbitrary_long_name==0
- 它应该一直失败.你可以用一些乱七八糟的字符串替换它.基本上,我正在 awk 中寻找一个不会对 shell 脚本产生不利影响的错误条件.
At first, I simply tried "exec" "/usr/bin/gawk" "--re-interval" "-f" "$0" "$@"
, but awk treated that as a command and printed out every line of input unconditionally. That is why I put in the arbitrary_long_name==0
- it's supposed to fail all the time. You could replace it with some gibberish string. Basically, I was looking for a false-condition in awk that would not adversely affect the shell script.
在 shell 脚本中,arbitrary_long_name==0
定义了一个名为 arbitrary_long_name
的变量,并将其设置为等于 =0
.
In the shell script, the arbitrary_long_name==0
defines a variable called arbitrary_long_name
and sets it equal to =0
.
这篇关于如何通过shebang(即#!)使用awk的多个参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!