Solved

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

Posted on 2009-05-15
8
964 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

732 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