如何在带有Shebang的awk中使用多个参数(即#!)? [英] How to use multiple arguments for awk with a shebang (i.e. #!)?

查看:121
本文介绍了如何在带有Shebang的awk中使用多个参数(即#!)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用shebang使用--re-interval执行 gawk 脚本.

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脚本中,或者制作一个外壳脚本,然后再调用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屋!

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