Reputation: 649
I'm trying to debug a shell script that calls an awk file. Which is a nightmare, cause I've never used either before, nor am I very fluent with linux, but anyway
A dev made an awk file and is trying to run it in a shell script.
To try and run it from a separate location, without needing to specify the exact location, they put the awk script in a folder that's in the PATH variable. So that awk file should be available everywhere, right?
When you run it like this...
awk -f script.awk arg1
...can awk find that script? It spits out an error, when the shell script tries to run that awk command:
awk: fatal: can't open source file `script.awk' for reading (No such file or directory)
Upvotes: 5
Views: 7900
Reputation: 754450
As you know, awk
can't find the script itself.
If the script is marked as executable, and if you have a which
command, then you should be able to do:
awk -f `which script.awk` arg1
Alternatively, and probably better, make the script into an executable:
#!/usr/bin/awk -f
BEGIN { x = 23 }
{ x += 2 }
END { print x }
The shebang needs the '-f
' option to work on MacOS X 10.7.1 where I tested it:
$ script.awk script.awk
31
$
That gives you a self-contained single-file solution.
Upvotes: 7
Reputation: 2061
No, that's not going to work. awk needs the path to the script to run, it won't use the PATH variable to find it. PATH is only used to find executables.
Upvotes: 2