如何从任何地方(任何目录)运行 perl 脚本 [英] How to run perl script from any where (any directory)
问题描述
我在以下路径中有一个 perl 脚本 (/home/Leen/Desktop/Tools/bin/tool.pl)每次我想运行这个工具时我都会去终端
I have a perl script exist in the follwoing path (/home/Leen/Desktop/Tools/bin/tool.pl) Every time I want to run this tool I go to the terminal
>
然后将目录更改为
..../bin>
然后我通过编写
..../bin> perl tool.pl file= whatever config= whatever
问题是我想运行这个 perl 脚本而不需要去它所在的 bin 文件夹.所以我可以从任何目录运行 perl 脚本,只要我进入 shell我去了 etc/environment 并写了以下内容
The problem is that I want to run this perl script without the need to go to the bin folder where it exist . so I can run perl script from any directory and as soon as I enter shell I went to the etc/environment and I wrote the follwoing
导出 PERL5LIB=$PERL5LIB:/home/Leen/Desktop/Tools/bin
export PERL5LIB=$PERL5LIB:/home/Leen/Desktop/Tools/bin
但是当我去终端并直接编写以下内容时,而无需进入 tool.pl 所在的 bin 文件夹
But when I go to terminal and write the follwoing straight ahead without going to bin folder where tool.pl exist
>perl tool.pl file=... config=...
它说文件tool.pl"不存在???
it says the file "tool.pl" does not exist???
推荐答案
perl
程序的第一个参数是可执行文件的路径.这些调用是等效的:
The first argument to the perl
program is the path to an executable file. These calls are equivalent:
:~$ perl /home/Leen/Desktop/Tools/bin/tool.pl
:~$ perl ~/Desktop/Tools/bin/tool.pl
:~$ perl ./Desktop/Tools/bin/tool.pl
:~/Desktop/Tools/bin$ perl tool.pl
:~/Desktop/Tools/bin$ perl ./tool.pl
等
在 shell 中,波浪号 ~
扩展到您的主目录,而 ./
表示当前目录.在 *nix shell(包括 ubuntu 上的各种终端模拟器)上,命令提示符通常是普通模式下的 $
,作为 root 用户的 #
和很少 %代码>.
>
是辅助命令提示符,例如继续多行参数时,与 Windows 上的 cmd.exe 不同.
In the shell the tilde ~
expands to your home directory, and ./
symbolizes the current directory. On *nix shells (including the various terminal emulators on ubuntu), the command prompt ususally is $
in nomal mode, #
as root user and seldom %
. >
Is a secondary command prompt, e.g. when continuing a multiline argument, unlike cmd.exe on Windows.
PERL5LIB
变量决定了 Perl 在哪里查找模块,而不是可执行文件.
The PERL5LIB
variable determines where Perl looks for modules, not for executable files.
您可以通过 chmod +x FILENAME
将脚本设置为可执行文件.然后您可以在不指定 perl
程序的情况下调用该脚本:
You can set a script as executable via chmod +x FILENAME
. You can then call the script without specifying the perl
program:
:~/Desktop/Tools/bin$ ./tool.pl
您可以修改 PATH
变量以更改 shell 查找可执行文件的位置.PATH
通常包含 /usr/bin/
和其他目录.您可以通过
You can modify the PATH
variable to change where the shell looks for executables. The PATH
usually contains /usr/bin/
and other directories. You can add a directory of your own via
PATH=$PATH:/home/Leen/Desktop/Tools/bin
在PATH
es的末尾添加你的目录,这样你就不会否决其他程序.
Add your directory at the end of the PATH
es, so you don't overrule other programs.
如果你想永久设置这个,你可以把这行添加到文件 ~/.bashrc
(只对你的用户和 bash
shell).
If you want to set this permanently, you can add this line to the file ~/.bashrc
(only for your user and only for the bash
shell).
然后您可以从任何地方调用您的脚本,而无需完整路径名:
Then you can call your script from anywhere, without a full path name:
:~/foo/bar$ tool.pl
在这种情况下,您应该考虑使用更具体的命令名称,以防止名称冲突.
You should consider using a more specific command name in this case, to prevent name clashes.
这篇关于如何从任何地方(任何目录)运行 perl 脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!