[tex-live] how robustly call awk script?

Heiko Oberdiek oberdiek at uni-freiburg.de
Sun Jan 14 21:29:14 CET 2007


On Sun, Jan 14, 2007 at 05:45:41PM +0000, Sanjoy Mahajan wrote:

> > It means that env took the "-f" option which you want to pass to awk.
> 
> Right.  I straced x.sh which is a simple script to total the numbers
> on stdin (I use it to unbalance my checkbook):
> 
> #!/usr/bin/env awk -f
> BEGIN {total=0};
> {total += $1}
> END {print total}
> 
> and it had these lines as it tried to find awk on the path:
> 
> ...
> execve("/usr/bin/awk -f", ["awk -f", "./x.sh"], [/* 36 vars */]) = -1
> ENOENT (No such file or directory)
> 
> So env stupidly combines the -f with the program name, making argv[0]
> = "awk -f", and searches for a binary named "/usr/bin/awk -f" (!)
> which will never be found.

>From the bash manual page:

|       If  the program is a file beginning with #!, the remainder
|       of the first line specifies an interpreter  for  the  pro­
|       gram.   The  shell  executes  the specified interpreter on
|       operating systems that do not handle this executable  for­
|       mat  themselves.  The arguments to the interpreter consist
|       of a single optional argument  following  the  interpreter
|       name  on  the  first  line of the program, followed by the
|       name of the program, followed by the command arguments, if
|       any.

"/usr/bin/env" is the interpreter, the rest of the line forms
the single (optional) argument: "awk -f"
  Thus with the /usr/bin/env trick you can specify a program,
but no further arguments.

Yours sincerely
  Heiko <oberdiek at uni-freiburg.de>


More information about the tex-live mailing list