简体   繁体   English

如何在 unix 环境中的任何位置运行 perl 脚本?

[英]How can i run perl script from anywhere in unix environment?

I have this perl script that I need to distribute to my coworkers who want to run the script from anywhere in the unix environment.我有这个 perl 脚本,我需要将它分发给想要从 unix 环境中的任何位置运行该脚本的同事。 What can I do on my part to make running this PERL script easy for them?我可以做些什么来让他们轻松运行这个 PERL 脚本? For example, they can just have the PERL script somewhere in their directory and run just typing例如,他们可以将 PERL 脚本放在其目录中的某个位置,然后只需键入即可运行

./xyz.pl ttt.conf

with no path declared (like /home/abc/bin/ddd/xyz.pl ttt.conf).没有声明路径(如 /home/abc/bin/ddd/xyz.pl ttt.conf)。

Put the script in /usr/local/bin (or anywhere else in $PATH ).将脚本放入/usr/local/bin (或$PATH中的任何其他位置)。 Your sysadmin may have to help you.您的系统管理员可能需要帮助您。

The way I used to do it is add a "bin" directory in your home directory, and add it to the $PATH variable.. then you can add any script you want to use to that directory.我以前这样做的方法是在您的主目录中添加一个“bin”目录,并将其添加到$PATH变量中。然后您可以将要使用的任何脚本添加到该目录中。

I am no longer familiar with the exact syntax, but something like:我不再熟悉确切的语法,但类似于:

in.bashrc:在.bashrc:

$PATH = ( $PATH , $HOME/bin )

Then place the script in /home/user/bin (assuming $HOME == /home/user).然后将脚本放在/home/user/bin中(假设 $HOME == /home/user)。 When you reload the shell, it will be usable like any normal command/program.当您重新加载 shell 时,它将像任何普通命令/程序一样使用。

ETA: See robert's comment below on syntax. ETA:请参阅下面的罗伯特关于语法的评论。 Also, to allow your co-workers to use a script of yours, you can simply use a hard-coded path, such as /home/patrick/bin .此外,为了让您的同事使用您的脚本,您可以简单地使用硬编码路径,例如/home/patrick/bin

The technique I use is:我使用的技术是:

#!/usr/bin/env perl

This is a common way of getting the command interpreter to find Perl without either (a) moving the file, or (b) declaring the explicit path for Perl in the shebang.这是让命令解释器找到 Perl 的常用方法,无需 (a) 移动文件,或 (b) 在 shebang 中声明 Perl 的显式路径。

It's mentioned under portability at: http://en.wikipedia.org/wiki/Shebang_(Unix )它在可移植性下提到: http://en.wikipedia.org/wiki/Shebang_(Unix )

You all are kind of right... but that perl script can sit in your path till the cows come home... and it ain't gonna run... until you set the executable bit....你们都是对的...但是 perl 脚本可以在您的路径中等待直到奶牛回家...它不会运行...直到您设置可执行位...。

:bin localadmin$ ./perlextip
-bash: ./perlextip: Permission denied
:bin localadmin$ chmod +x perlextip 
:bin localadmin$ ./perlextip 
Exit 0!  Yeehaw.

Also, it should be noted that it need not be IN your path.... You can just call it by the full path, preceeded with a period and a slash, to execute it..另外,应该注意的是它不需要在你的路径中......你可以通过完整的路径调用它,前面有一个句点和一个斜杠,以执行它..

:/ localadmin$ ./ServiceData/UNIX/bin/extip
Exit 0!  Yeehaw.

You can also create an alias for such a command in your ~/.bash_profile , or the such, which will let you make a system-wide shortcut of sorts, and you can even throw in a sudo, or the like, if you were so inclined... Then just call that "extip" by name anywhere, you'll be prompted for a password and, all will be well in the world.您还可以在~/.bash_profile或类似的命令中为此类命令创建别名,这将使您可以创建系统范围的各种快捷方式,您甚至可以输入 sudo 等,如果您是如此倾向......然后只需在任何地方按名称调用“extip”,系统就会提示您输入密码,并且一切都会好起来的。

alias extip='sudo ./ServiceData/UNIX/bin/extip'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM