4

I'm trying to make an awk file executable. I've written the script, and did chmod +x filename. Here is the code:

#!/bin/awk -v

'TOPNUM = $1

## pick1 - pick one random number out of y
## main routine

BEGIN {
## set seed
        srand ()

## get a random number

        select = 1 +int(rand() * TOPNUM)

# print pick
        print select
}'

When I try and run the program and put in a variable for the TOPNUM:

pick1 50

I get the response:

-bash: /home/petersone/bin/pick1: /bin/awk: bad interpreter: No such file or directory

I'm sure that there's something simple that I'm messing up, but I simply cannot figure out what it is. How can I fix this?

Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
Eric
  • 380
  • 5
  • 14
  • 3
    Is `awk` really installed to `/bin/awk`? –  Apr 22 '13 at 17:18
  • 2
    try "which awk" to see where akw is or put #!/usr/bin/env awk at the top instead of #!/bin/awk. If which awk doesn't show anything you need to install awk. – Paul Rubel Apr 22 '13 at 17:20
  • 1
    See [this question](http://unix.stackexchange.com/q/29608/10454) and [my answer](http://unix.stackexchange.com/a/29620/10454) for a discussion of the pros and cons of the `#!/usr/bin/env` trick. And note that you can't use `#!/usr/bin/env awk -v`; it doesn't take multiple arguments. – Keith Thompson Apr 22 '13 at 17:36
  • [This question](http://stackoverflow.com/questions/4303128/how-to-use-multiple-arguments-with-a-shebang-i-e) discusses how to pass a command-line argument to awk. So does [this question](http://unix.stackexchange.com/questions/97141/distributing-a-script-should-i-use-bin-gawk-or-usr-bin-gawk-for-shebang). – mernst Apr 10 '15 at 12:10

2 Answers2

5

From a command line, run this command:

which awk

This will print the path of AWK, which is likely /usr/bin/awk. Correct the first line and your script should work.

Also, your script shouldn't have the single-quote characters at the beginning and end. You can run AWK from the command line and pass in a script as a quoted string, or you can write a script in a file and use the #!/usr/bin/awk first line, with the commands just in the file.

Also, the first line of your script isn't going to work right. In AWK, setup code needs to be inside the BEGIN block, and $1 is a reference to the first word in the input line. You need to use ARGV[1] to refer to the first argument.

http://www.gnu.org/software/gawk/manual/html_node/ARGC-and-ARGV.html

As @TrueY pointed out, there should be a -f on the first line:

#!/usr/bin/awk -f

This is discussed here: Invoking a script, which has an awk shebang, with parameters (vars)

Working, tested version of the program:

#!/usr/bin/awk -f

## pick1 - pick one random number out of y
## main routine
BEGIN {
    TOPNUM = ARGV[1]

## set seed
        srand ()

## get a random number

        select = 1 +int(rand() * TOPNUM)

# print pick
        print select
}
Community
  • 1
  • 1
steveha
  • 74,789
  • 21
  • 92
  • 117
3

Actually this form is more preferrable:

#! /bin/awk -E 

Man told:

-E Similar to -f, however, this is option is the last one processed and should be used with #! scripts, particularly for CGI applications, to avoid passing in options or source code (!) on the command line from a URL. This option disables command-line variable assignments

ma5ter
  • 41
  • 2
  • This is a good point, but it doesn't answer the question. It should have been a comment. (But you need at least 50 rep points to post comments on others' posts.) – Keith Thompson Jun 21 '18 at 18:01