1

I have main.sh and external.awk files.

They are in the same directory.

From main.sh I am calling that external awk script like this:

awk -f external.awk ..

and of course it is working.

Now when I do:

cd /usr/local/bin
sudo ln -s /path/to/scripts/dir/main.sh main.sh
sudo ln -s /path/to/scripts/dir/external.awk external.awk

I can call my main.sh from whatever directory I am in. But it gives me error on not being able to find external.awk script.

Why linking does not work in this case, shouldn't that

awk -f external.awk ..

call external.awk relative to folder where it is, thus in this case calling that symbolic link which is in /usr/local/bin path?

EDIT

Soon after posting my question, I found this as a good way to handle this situation:

https://www.gnu.org/software/gawk/manual/html_node/AWKPATH-Variable.html

It is not POSIX compliant, but in this case it is not of an importance to me.

branquito
  • 3,864
  • 5
  • 35
  • 60
  • 1
    No, it will try to call the AWK script from *your* current directory, not the directory where the shell script is. See e.g. [this old question](http://stackoverflow.com/questions/242538/unix-shell-script-find-out-which-directory-the-script-file-resides) to find a way to solve it. – Some programmer dude Jul 14 '14 at 14:47
  • Is there a way I can set `path` so that `external.awk` is found this way? – branquito Jul 14 '14 at 14:52
  • 1
    It works for the shell script because the shell uses `$PATH` to find programs to run. It doesn't work for `awk` because the argument to `-f` is the pathname leading to the script file. When you're in the same directory, `external.awk` is the name of the file in the current directory. When you're somewhere else, then `-f external.awk` still specifies `./external.awk`, but the file doesn't exist. If you have a script `pathfile`, which hunts a file on a path, you could use `awk -f $(pathfile external.awk) …` to locate the file for you. If you don't have such a script, you could write one. – Jonathan Leffler Jul 14 '14 at 14:53
  • 1
    @JoachimPileborg I think this might be the better and much cleaner soulution to the problem. https://www.gnu.org/software/gawk/manual/html_node/AWKPATH-Variable.html It applies to `gawk` obviously, but that is what I am working with. – branquito Jul 14 '14 at 14:57
  • +1 for the discovery and documentation of GNU `awk`'s AWKLIBPATH. It does precisely what you want, and if you'll always be using GNU `awk`, it makes sense to use it. – Jonathan Leffler Jul 14 '14 at 15:49

1 Answers1

0

Modify main.sh to call awk as this:

awk -f /usr/local/bin/external.awk ..