bash 在命令行中执行 perl 而不在 UNIX 中指定 perl
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9009157/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
execute perl in command line without specifying perl in UNIX
提问by Chris
There is a similar question about Windows, but it won't work for Unix based computers (OS X specifically).
有一个关于 Windows 的类似问题,但它不适用于基于 Unix 的计算机(特别是 OS X)。
I want to type the name of a file, say example.pl
or example.pl parametertext.txt
, and have it know to execute perl.
我想输入一个文件的名称,比如example.pl
or example.pl parametertext.txt
,让它知道执行 perl。
I specified #!/usr/bin/perl
in the file so it can find the executable. Instead, I get the message:
我#!/usr/bin/perl
在文件中指定,以便它可以找到可执行文件。相反,我收到消息:
bash: example.pl: command not found
回答by Shiplu Mokaddim
You can do it this way,
你可以这样做,
- Find the interpreter/executors path. In this case its
/usr/bin/perl
or/usr/bin/env perl
- Add it to the first line of the file as
#!/usr/bin/perl
. - Give execute permission to the file
chmod +x example.pl
- 找到解释器/执行器路径。在这种情况下,其
/usr/bin/perl
或/usr/bin/env perl
- 将其添加到文件的第一行作为
#!/usr/bin/perl
. - 赋予文件执行权限
chmod +x example.pl
Now it will run
现在它会运行
$ ./example.pl
回答by Kevin
You need to make the top line (the "shebang") #!/usr/bin/perl
(note the slash where you have a space). Then, first you need to make sure that is actually the correct path to your perl
executable (type which perl
to see where it is). If it's elsewhere, correct the path appropriately. Then you need to make sure the script has the execute permission set. Type ls -l example.pl
, and look for an x
in the first column (fourth character, in particular). If it's not there, you need to make the script executable with chmod a+x example.pl
. Finally, to run the program, you need to use ./example.pl
. The ./
tells your shell that you want the script in your current directory.
您需要制作顶线(“shebang”)#!/usr/bin/perl
(注意有空格的斜线)。然后,首先您需要确保它实际上是您的perl
可执行文件的正确路径(键入which perl
以查看它的位置)。如果它在其他地方,请适当更正路径。然后您需要确保脚本具有执行权限集。键入ls -l example.pl
,然后x
在第一列(特别是第四个字符)中查找 an 。如果它不存在,您需要使用chmod a+x example.pl
. 最后,要运行该程序,您需要使用./example.pl
. 该./
告诉你的shell要在当前目录的脚本。
回答by user1126070
I have two thoughts to add:
我有两个想法要补充:
1) To use example.pl test1.txt your path should contain a dot. echo $PATH /usr/bin:/usr/X11R6/bin:/usr/bin/X11:.
1) 要使用 example.pl test1.txt,您的路径应包含一个点。echo $PATH /usr/bin:/usr/X11R6/bin:/usr/bin/X11:。
2) Your file end of line should be unix, \n. At least your shebang line contain excatly your perl path, ended with a \n.
2)你的文件行尾应该是unix,\n。至少你的shebang行包含你的perl路径,以\ n结尾。