How to use multiple arguments for awk with a shebang (i.e. #!)? Ask Question

How to use multiple arguments for awk with a shebang (i.e. #!)? Ask Question

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

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?

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.

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.

ベストアンサー1

The shebang line has never been specified as part of POSIX, SUS, LSB or any other specification. AFAIK, it hasn't even been properly documented.

There is a rough consensus about what it does: take everything between the ! and the \n and exec it. The assumption is that everything between the ! and the \n is a full absolute path to the interpreter. There is no consensus about what happens if it contains whitespace.

  1. Some operating systems simply treat the entire thing as the path. After all, in most operating systems, whitespace or dashes are legal in a path.
  2. 一部のオペレーティング システムでは、空白で分割し、最初の部分をインタープリターへのパスとして扱い、残りを個別の引数として扱います。
  3. 一部のオペレーティングシステムは、初め空白を取り除いて、先頭部分をインターペーターへのパスとして扱い、残りをシングル議論(あなたが見ているもの)
  4. 中にはシェバンラインをサポートしていないものもあるまったく

ありがたいことに、1. と 4. は廃れてきたようですが、3. はかなり普及しているため、複数の引数を渡すことができると単純に期待することはできません。

また、POSIXやSUSではコマンドの場所も指定されていないため、通常は実行ファイルの名前envそうするためにそれ実行可能ファイルの場所を特定できます。例:

#!/usr/bin/env gawk

[明らかに、これはまだは の特定のパスを想定していますenvが、 に存在するシステムは非常に少ないため/bin、これは一般的に安全です。 の場所はenvの場所よりもはるかに標準化されています。さらに悪いことに、やや のgawkようなものも標準化されています。pythonrubyspidermonkey

つまり、実際には使用できないどれでも議論まったく

おすすめ記事