在 UNIX 中不指定 perl 在命令行中执行 perl
关于 Windows 也有类似的问题,但它不适用于基于 Unix 的计算机(特别是 OS X)。
我想输入一个文件的名称,例如 example.pl
或 example.plparametertext.txt
,并让它知道执行 perl。
我在文件中指定了#!/usr/bin/perl
,以便它可以找到可执行文件。相反,我收到消息:
bash: example.pl: command not found
There is a similar question about Windows, but it won't work for Unix based computers (OS X specifically).
I want to type the name of a file, say example.pl
or example.pl parametertext.txt
, and have it know to execute perl.
I specified #!/usr/bin/perl
in the file so it can find the executable. Instead, I get the message:
bash: example.pl: command not found
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以这样做,
/usr/bin/perl
或/usr/bin/env perl
#!/usr/ bin/perl
.,现在它将运行
You can do it this way,
/usr/bin/perl
or/usr/bin/env perl
#!/usr/bin/perl
.chmod +x example.pl
Now it will run
您需要制作顶行(“shebang”)
#!/usr/bin/perl
(注意有空格的斜线)。然后,首先您需要确保这实际上是perl
可执行文件的正确路径(输入which perl
以查看它在哪里)。如果在其他地方,请适当更正路径。然后您需要确保该脚本具有执行权限集。输入ls -l example.pl
,然后在第一列(特别是第四个字符)中查找x
。如果不存在,您需要使用 chmod a+x example.pl 使脚本可执行。最后,要运行该程序,您需要使用./example.pl
。./
告诉您的 shell 您希望该脚本位于当前目录中。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 yourperl
executable (typewhich 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. Typels -l example.pl
, and look for anx
in the first column (fourth character, in particular). If it's not there, you need to make the script executable withchmod 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.我有两个想法要补充:
1)要使用 example.pl test1.txt 你的路径应该包含一个点。
回显$PATH
/usr/bin:/usr/X11R6/bin:/usr/bin/X11:。
2) 你的文件行尾应该是unix, \n。至少你的 shebang 行完全包含你的 perl 路径,以 \n 结尾。
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:.
2) Your file end of line should be unix, \n. At least your shebang line contain excatly your perl path, ended with a \n.