开发者

Run awk file from shell script without specifying awk's exact location

开发者 https://www.devze.com 2023-03-31 04:57 出处:网络
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

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)


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号