Solved

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

Posted on 2009-05-15
8
935 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 11

Assisted Solution

by:climbgunks
climbgunks earned 100 total points
Comment Utility

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:przach
Comment Utility
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
Comment Utility

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
Comment Utility
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
Comment Utility
Great!!! That worked. Thank you all.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Setting up Secure Ubuntu server on VMware 1.      Insert the Ubuntu Server distribution CD or attach the ISO of the CD which is in the “Datastore”. Note that it is important to install the x64 edition on servers, not the X86 editions. 2.      Power on th…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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 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…

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now