Solved

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

Posted on 2009-05-15
8
969 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
Does Your Cloud Backup Use Blockchain Technology?

Blockchain technology has already revolutionized finance thanks to Bitcoin. Now it's disrupting other areas, including the realm of data protection. Learn how blockchain is now being used to authenticate backup files and keep them safe from hackers.

 
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

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

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…
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
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.:

630 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