Solved

How do I run a perl script on Linux without typing 'perl'

Posted on 2009-05-15
8
956 Views
Last Modified: 2013-12-16
I have a perl script which can be run on Linux by typing 'perl  script.pl  arg'. But I want to run it without having to type the word 'perl' like this 'script.pl  args'. There is an already existing file which runs without typing 'perl', so I know that there are no system settings required to be done. Also, I have included #usr/bin/perl and also changed the chmod to 755. None of this seems to work.
0
Comment
Question by:przach
8 Comments
 
LVL 28

Expert Comment

by:FishMonger
ID: 24400456
The first line in the file is called the "shebang line" and holds the full path to perl

A common shebang line would be:
#!/usr/bin/perl

http://en.wikipedia.org/wiki/Shebang_(Unix)

The path may be different on your system.

If you're in the same directory as the script, you need to specify the path to the script, either full or relative path.
[root@fc4dev ~]# ./test.pl
0
 

Author Comment

by:przach
ID: 24400475
I tried '>which perl' and it returned #!usr/local/bin/perl. I tried changing the shebang line to #!usr/local/bin/perl and it still doesnt work. But I do not understand where and how to specify the path as you have shown. I am in the same directory as my script.
0
 
LVL 2

Expert Comment

by:malklavious
ID: 24400512
Make sure the permissions of the file are set to executable.  When you run the script you may need to start the command with "./"

ie:

./myScript.pl
0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
LVL 11

Assisted Solution

by:climbgunks
climbgunks earned 100 total points
ID: 24400515

For you,as FishMonger said, the first line of your perl script should then be

#!/;usr/local/bin/perl

In addtion, you have to make your script executable

chmod a+x script.pl

then you can either type the bare name of the script (if it is on your PATH), or give the full or relative path to your script

script.pl
./script.pl
/home/mydir/script.pl


what error are you getting?


0
 

Author Comment

by:przach
ID: 24400525
I have changed the mode to executable. When I try giving:
./script.pl <args>

it does not accept args. It says this:
No such file or directory at line 19. And my line 19 is:
open INPUT, $input or die $!;

But if I run the script without ./ it says command not found.
0
 
LVL 11

Accepted Solution

by:
climbgunks earned 100 total points
ID: 24400535

we've solved the executability problem...you need to use ./script.pl

the other problem is simply a perl coding problem

you're probably doing something like this to find the file you want to open


$input = <>;

you probably need to get rid of the newline at the end of the string

so, use:

$input = <>;
chomp($input);
0
 
LVL 30

Assisted Solution

by:Kerem ERSOY
Kerem ERSOY earned 25 total points
ID: 24400545
If you get an error on line 19 it measn that You've been running your script so your problem has ben solved !!! Congratulations. As to why you get an error at line 19 is probably ou're running the program from your current path in most cases it is your home directory that your file is not in there but under another folder.

./  means th program is under your current path and unlike windows current path is not included in your default path. To include the current path to the search path you can type this command:

export PATH=$PATH:.

Then it will run the script from current directory without ./ . If you want to mate this beaviour defualt then modefy the PATH statement in your .bashrc.

Cheers,
K.
0
 

Author Comment

by:przach
ID: 24400547
Great!!! That worked. Thank you all.
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction We as admins face situation where we need to redirect websites to another. This may be required as a part of an upgrade keeping the old URL but website should be served from new URL. This document would brief you on different ways ca…
Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question