Run awk file from script shell without specifying awk exact location

I am trying to debug a shell script that calls an awk file. This is a nightmare because I have never used it before, and I'm not very fluent in Linux, but anyway

Dev created an awk file and tries to run it in a shell script.

To try to run it from a separate location, without specifying the exact location, they put the awk script in a folder that is in the PATH variable. So the awk file should be available everywhere, right?

When you run it like this:

awk -f script.awk arg1

... can awk find that script? An error spits out when the shell script tries to run this awk command:

awk: fatal: cannot open the source file `script.awk 'for reading (there is no such file or directory)

+4
source share
2 answers

As you know, awk cannot find the script itself.

If the script is marked as an executable, and if you have a which command, you should be able to:

 awk -f `which script.awk` arg1 

Alternatively and probably better, make a script into an executable:

 #!/usr/bin/awk -f BEGIN { x = 23 } { x += 2 } END { print x } 

Shebang needs the -f option to work with MacOS X 10.7.1, where I tested it:

 $ script.awk script.awk 31 $ 

This gives you a standalone single phase solution.

+7
source

No, that will not work. awk needs a script path to run, it will not use the PATH variable to find it. PATH is used only to search for executable files.

+2
source

Source: https://habr.com/ru/post/1369180/


All Articles