“不良口译员"尝试运行awk可执行文件时出现错误消息 [英] "bad interpreter" error message when trying to run awk executable

查看:106
本文介绍了“不良口译员"尝试运行awk可执行文件时出现错误消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使awk文件可执行.我已经编写了脚本,并做了chmod +x filename.这是代码:

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
}'

当我尝试运行程序并为TOPNUM放入变量时:

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

pick1 50

我得到答复:

-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?

推荐答案

从命令行运行以下命令:

From a command line, run this command:

which awk

这将打印AWK的路径,可能是/usr/bin/awk.更正第一行,您的脚本应该可以正常工作.

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

此外,您的脚本的开头和结尾不应包含单引号字符.您可以从命令行运行AWK并以带引号的字符串传递脚本,也可以在文件中编写脚本并使用#!/usr/bin/awk第一行,而命令仅位于文件中.

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.

此外,脚本的第一行无法正常工作.在AWK中,设置代码必须位于BEGIN块内,并且$1是对输入行中第一个单词的引用.您需要使用ARGV[1]来引用第一个参数.

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

@TrueY指出,第一行应该有-f:

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

#!/usr/bin/awk -f

在此处进行讨论:调用具有awk shebang并带有参数(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
}

这篇关于“不良口译员"尝试运行awk可执行文件时出现错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆