Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

unix

perl

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. What can I do on my part to make running this PERL script easy for them? For example, they can just have the PERL script somewhere in their directory and run just typing

./xyz.pl ttt.conf

with no path declared (like /home/abc/bin/ddd/xyz.pl ttt.conf).

like image 606
RnD Avatar asked Jul 09 '11 21:07

RnD


4 Answers

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.

I am no longer familiar with the exact syntax, but something like:

in .bashrc:

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

Then place the script in /home/user/bin (assuming $HOME == /home/user). When you reload the shell, it will be usable like any normal command/program.

ETA: See robert's comment below on syntax. 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.

like image 78
TLP Avatar answered Nov 03 '22 06:11

TLP


Put the script in /usr/local/bin (or anywhere else in $PATH). Your sysadmin may have to help you.

like image 26
robert Avatar answered Nov 03 '22 04:11

robert


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.

It's mentioned under portability at: http://en.wikipedia.org/wiki/Shebang_(Unix)

like image 43
Stuart Watt Avatar answered Nov 03 '22 04:11

Stuart Watt


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....

: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.

alias extip='sudo ./ServiceData/UNIX/bin/extip'
like image 34
Alex Gray Avatar answered Nov 03 '22 04:11

Alex Gray