如何修复“bash: perl myscript.pl: command not found”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1800148/
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
How do I fix "bash: perl myscript.pl: command not found"?
提问by Nii
Maybe it's dumbest question in the world, but I seriously have problems with it and could use help. I am trying to run perl script on linux. It's a simple text editing script, nothing fancy. I googled for it and I found that I had to chmod +xit and then just run myscript.plin the console. Since it's supposed to modify a text file I did myscript.pl > myfile.txtafter chmoding it
也许这是世界上最愚蠢的问题,但我对它有严重的问题,可以寻求帮助。我正在尝试在 linux 上运行 perl 脚本。这是一个简单的文本编辑脚本,没什么特别的。我用谷歌搜索它,我发现我必须这样chmod +x做,然后才myscript.pl在控制台中运行。由于它应该修改我myscript.pl > myfile.txt在 chmoding 后所做的文本文件
But it doesn't work. I get: bash: perl myscript.pl: command not found
但它不起作用。我得到:bash: perl myscript.pl: command not found
回答by EmFi
Unless myscript.pl is in your path you will need to specify the current directory.
除非 myscript.pl 在您的路径中,否则您需要指定当前目录。
$ ./myscript.pl
You can check if the current directory is in your path with $ echo $PATH. If you're frequently using this script you can put it in the path by moving it to a directory that's part of your path, usually ~/bin.
您可以检查当前目录是否在您的路径中$ echo $PATH。如果您经常使用此脚本,则可以通过将其移动到作为路径一部分的目录(通常为~/bin.
Or by adding the current directory to the $PATH environment variable. Check the documentation for your shell for instructions.
或者通过将当前目录添加到 $PATH 环境变量中。查看您的 shell 的文档以获取说明。
回答by Chirael
Can you post the first few lines of your script?
您可以发布脚本的前几行吗?
Specifically, if you have #!/usr/bin/perlare there any typos on that line, extra spaces, etc.?
具体来说,如果您#!/usr/bin/perl在该行上有任何拼写错误、多余的空格等?
Also do a ls /usr/bin/perl(or whatever is on that line) to make sure it's actually there.
还做一个ls /usr/bin/perl(或该行上的任何内容)以确保它确实在那里。
回答by Ether
As Chirael said, it sounds like your shebang line (the directive at the top of the file, that tells the shell how to run the script) is invalid somehow. You can bypass the shebang line entirely by invoking your script as:
正如 Chirael 所说,听起来您的 shebang 行(文件顶部的指令,告诉 shell 如何运行脚本)以某种方式无效。您可以通过调用脚本来完全绕过 shebang 行:
perl myscript.pl > myfile.txt
You also don't need to set the script's executable bit, as with this method of invocation, you are only reading the script, not executing it (from the shell's perspective).
您也不需要设置脚本的可执行位,因为使用这种调用方法,您只是在读取脚本,而不是执行它(从 shell 的角度来看)。
回答by BryanD
It doesn't look like perl is installed on your Linux machine. Do you get the same thing when you try this: # perl -e 'print "hi";'?
你的 Linux 机器上似乎没有安装 perl。当你尝试这个时,你得到同样的东西: # perl -e 'print "hi";'?
回答by ccheneson
回答by Paul Mc
I had the same issue, and traced it to DOS line endings (^M). Running dos2unix on the .pl file fixed the issue.
我遇到了同样的问题,并将其追溯到 DOS 行尾 (^M)。在 .pl 文件上运行 dos2unix 修复了该问题。
回答by Shashanka D
Please use, ./myperl.pl > outfile.txt to give the current directory path thanks
请使用 ./myperl.pl > outfile.txt 给出当前目录路径谢谢

