Wahedsaw
Wahedsaw

Reputation: 397

How to run perl script from any where (any directory)

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

>

and then change the directory to

..../bin>

Then I run the perl by writing

..../bin> perl tool.pl file= whatever config= whatever

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

export PERL5LIB=$PERL5LIB:/home/Leen/Desktop/Tools/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=...

it says the file "tool.pl" does not exist???

Upvotes: 2

Views: 29470

Answers (1)

amon
amon

Reputation: 57650

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

etc.

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.

The PERL5LIB variable determines where Perl looks for modules, not for executable files.

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

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

Add your directory at the end of the PATHes, so you don't overrule other programs.

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.

Upvotes: 9

Related Questions