Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!utgpu!water!watmath!clyde!rutgers!labrea!decwrl!sun!gorodish!guy From: guy@gorodish.UUCP Newsgroups: comp.unix.questions Subject: Re: executable awk scripts Message-ID: <28291@sun.uucp> Date: Thu, 17-Sep-87 00:06:11 EDT Article-I.D.: sun.28291 Posted: Thu Sep 17 00:06:11 1987 Date-Received: Sat, 19-Sep-87 08:14:04 EDT References: <1881@vax135.UUCP> Sender: news@sun.uucp Distribution: na Lines: 33 > Given that these files are executable, why does this fail: > > #!/bin/awk > { print $1 } Because, if this file is called "foo", and you say "foo", it is as if you had said "/bin/awk foo". This tells it to read "foo" as its *input* file, not as its *program* file, and gives it no program; since it had no program, it issues its all-purpose error message "awk: syntax error near line N" followed by "awk: bailing out near line N". A "#!" script is run by taking the argument list to the original "exec", shifting it right by one, sticking in the name of the script at the left and, if an argument appeared on the "#!" line, shifting the list right one more and sticking that argument in on the front. Thus, if "foo" contained #! /bin/awk -f { print $1 } saying "foo" would run "/bin/awk -f foo", which is what you want. > FYI: tested under two BSD variants (sorry, no SysV): > Sun UNIX 4.2 Release 3.3 > Ultrix V2.0-1 System #1: Wed Jul 15 10:28:13 EDT 1987 Most System V systems have probably not picked up "#!" - none of AT&T's have, as far as I know - so it would have been hard to test it there. Note, though, that the SunOS 3.3 "awk" is a bug-fixed version of the S5R2 "awk" (which is noticeably faster than the "awk" that comes with 4BSD, which is somewhere between the V7 and S5R2 one along the "awk" timeline). Guy Harris {ihnp4, decvax, seismo, decwrl, ...}!sun!guy guy@sun.com