-1

I have written an awk file and made it executable, but do not want to call my awk file in the following manner

awk -f tool.awk FILE

I just want to call it using

tool FILE

I made tool.awk executable and included

#! /bin/awk -f

Followed by a link named tool to tool.awk.

I could then call tool FILE

I suppose I can also call tool FILE in a bash script as well. Right?

Dilna
  • 405
  • 1
  • 6

1 Answers1

3

Don't do that. Create a shell script named tool and call awk from it:

#!/usr/bin/env bash

awk 'script' "${@:--}"

If you want to call awk from a bash function named foo:

#!/usr/bin/env bash

foo() {
    awk 'script' "${@:--}"
}

foo "${@:--}"

File suffixes are meaningless in UNIX but if you create a command that ends in any suffix, e.g. tool.awk then that's exactly what you need to type to call it (but don't do that as UNIX commands shouldn't have suffixes).

Ed Morton
  • 188,023
  • 17
  • 78
  • 185
  • Can I do a bash function instead? – Dilna Feb 20 '23 at 22:04
  • Can I do a bash function instead? How would it look like ? – Dilna Feb 20 '23 at 22:20
  • I added some code to show how to call awk from a function. – Ed Morton Feb 20 '23 at 22:30
  • For what reason do you employ `"${@:--}"`? Would the awk script need `#!/bin/awk -f` ? – Dilna Feb 20 '23 at 22:39
  • `"${@:--}"` is a shell construct that means `use any arguments passed in or stdin otherwise` so a script or function can operate on a file or input coming from a pipe. – Ed Morton Feb 20 '23 at 22:54
  • `#!/bin/awk -f` is not applicable because, again, you should not call awk with a shebang, see https://stackoverflow.com/a/61002754/1745001. Call shell scripts with a shebang that identifies the shell, and just directly call awk from the shell script, as shown in my answer, to avoid unnecessary complications. – Ed Morton Feb 20 '23 at 22:57
  • Then try `awk -f ${awkpath}/firefly.awk "${@:--}"` . Good luck. – shellter Feb 20 '23 at 23:53
  • 1
    @EdMorton, very nice code, cheers sir. – RavinderSingh13 Feb 21 '23 at 04:14
  • What does `the ability to separate arguments passed to your shell script into values for the shell to process` mean exactly. I do have a function that I can call. Could one call it with arguments relevant to the awk file without restrictions? – Dilna Feb 21 '23 at 08:43
  • The answer I referenced in [my comment](https://stackoverflow.com/questions/75513719/calling-awk-tool-directly-by-name/75514484?noredirect=1#comment133233519_75514484) explains the issue but if you have any questions about that answer then please leave them under that answer rather than copying some of the content of that answer into a comment here and then asking about it. – Ed Morton Feb 21 '23 at 10:19