我试图做一个awk文件可执行。 我写剧本,并没有chmod +x filename
。 下面是代码:
#!/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
:
pick1 50
我得到的回应:
-bash: /home/petersone/bin/pick1: /bin/awk: bad interpreter: No such file or directory
我敢肯定,有一些简单的我搞乱了,但我根本无法弄清楚它是什么。 我怎样才能解决这个问题?
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
}
其实这种形式更preferrable:
#! /bin/awk -E
男子告诉:
-E类似-F,然而,这选项是最后一个处理,应#使用! 脚本,特别是对于CGI应用程序,以避免传递选项或源代码(!)从URL在命令行上。 该选项禁用命令行变量赋值