LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-14-2010, 04:34 PM   #1
sentez_mon_fromage
LQ Newbie
 
Registered: Mar 2010
Posts: 4

Rep: Reputation: 0
Perl Setup on Linux


Hi,

I'm a Linux newbie and I have a bit of an annoying problem with running Perl scripts in Linux.

My system is Ubuntu (Karmic Koala distro) and the problem I have is that Perl scripts I have written do not seem like they are being associated with the perl executable when I try to run them in a terminal.

Basically I want to run a Perl script like the following command:

% hello_world.pl

But I am forced to run it as the following:

% perl hello_world.pl

The problem would seem at first to be with my shebang line. However, I am fairly confident that this is set up correctly. If I run the following, perl -e 'print "$^X\n;"', I can see that the path to the interpreter is 'usr/bin/perl'. Thus my shebang is #!/usr/bin/perl.

Yet running 'hello_world.pl' gives the following response : hello_world.pl: command not found

I am stuck. Do people reckon the problem is due to me not setting up my .bashrc file correctly? Why doesn't Linux recognise, or associate, my shebang with the perl executable?

Many thanks in advance,
 
Old 03-14-2010, 06:14 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Hi, welcome to LQ!

What shell are you working with?


Cheers,
Tink
 
Old 03-14-2010, 06:33 PM   #3
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,608

Rep: Reputation: 448Reputation: 448Reputation: 448Reputation: 448Reputation: 448
Quote:
Originally Posted by sentez_mon_fromage View Post
Hi,

I'm a Linux newbie and I have a bit of an annoying problem with running Perl scripts in Linux.

My system is Ubuntu (Karmic Koala distro) and the problem I have is that Perl scripts I have written do not seem like they are being associated with the perl executable when I try to run them in a terminal.

Basically I want to run a Perl script like the following command:

% hello_world.pl

But I am forced to run it as the following:

% perl hello_world.pl

The problem would seem at first to be with my shebang line. However, I am fairly confident that this is set up correctly. If I run the following, perl -e 'print "$^X\n;"', I can see that the path to the interpreter is 'usr/bin/perl'. Thus my shebang is #!/usr/bin/perl.

Yet running 'hello_world.pl' gives the following response : hello_world.pl: command not found

I am stuck. Do people reckon the problem is due to me not setting up my .bashrc file correctly? Why doesn't Linux recognise, or associate, my shebang with the perl executable?

Many thanks in advance,
Hi,

I don't know about perl, but don't you have to call a script like
Code:
./hello_world.pl
It is a security measure in linux that you have to explicitly tell your shell to execute a program in your current directory. That is what './' is for. I assume execution permission is set correctly.

Last edited by crts; 03-14-2010 at 06:37 PM.
 
Old 03-14-2010, 06:51 PM   #4
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 18,336

Rep: Reputation: 3896Reputation: 3896Reputation: 3896Reputation: 3896Reputation: 3896Reputation: 3896Reputation: 3896Reputation: 3896Reputation: 3896Reputation: 3896Reputation: 3896
Quote:
Originally Posted by crts View Post
Hi,
I don't know about perl, but don't you have to call a script like
Code:
./hello_world.pl
It is a security measure in linux that you have to explicitly tell your shell to execute a program in your current directory. That is what './' is for. I assume execution permission is set correctly.
It's not a security measure, but a matter of the shell not being able to find the script.

The PATH environment variable is what the shell looks at. If you were to copy your script into any of the directories that your path contains, it'll run just fine. The "./" merely states the explicit path, being where you are now. If your script was in /software/mytest/directory/myscript.pl, you could either type that entire thing in, or cd into that directory, and type in ./myscript.pl

With a Perl program, *MOST* of the time you put in a "#!/usr/bin/perl" (or wherever your Perl program is located), as the first line of the script. If you do a "chmod 755" on the .pl file, you can just run it by typing in the script name. If you don't have that as the first line, or need some explicit Perl run-time things, you can run it as "perl <script name>".
 
Old 03-14-2010, 07:06 PM   #5
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,608

Rep: Reputation: 448Reputation: 448Reputation: 448Reputation: 448Reputation: 448
Quote:
It's not a security measure, but a matter of the shell not being able to find the script.
I know opinions differ wether one should include the current directory in your PATH or not, e.g., malicious code that 'disguises' itself as a normal command. I for myself decided not to include it in my PATH and it is a good thing that the shell does not look in the current directory by default. This would be the intuitive behaviour.
 
Old 03-14-2010, 08:13 PM   #6
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721
The problem is that the shell does not look into your current working dir for commands.

You must explicitly tell it to look in this dir by prepending "./" to the script name.
 
Old 03-14-2010, 08:55 PM   #7
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,260

Rep: Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328
You definitely have to provide the path (whether relative or absolute) to any cmd/script if its location is not in your $PATH variable. By convention, the curent dir (.) and your home dir are not in $PATH (for security reasons).
 
Old 03-15-2010, 02:27 AM   #8
rockfall
LQ Newbie
 
Registered: Mar 2010
Posts: 11

Rep: Reputation: 0
type export PATH=$PATH:. or system real hard use
 
Old 03-15-2010, 02:42 AM   #9
sentez_mon_fromage
LQ Newbie
 
Registered: Mar 2010
Posts: 4

Original Poster
Rep: Reputation: 0
Firstly, I am overwhelmed by the number of responses here ...

Quote:
Originally Posted by Tinkster View Post
Hi, welcome to LQ!

What shell are you working with?

Cheers,
Tink
I am working with a bash shell (/bin/bash)

Quote:
Originally Posted by crts View Post
Hi,

I don't know about perl, but don't you have to call a script like
Code:
./hello_world.pl
It is a security measure in linux that you have to explicitly tell your shell to execute a program in your current directory. That is what './' is for. I assume execution permission is set correctly.
Hi, I have tried running './hello_world.pl' but the same error returns (hello_world.pl: command not found). The script has been 'chmod +x'ed for executable so the permission should be set correctly.

Quote:
Originally Posted by TB0ne View Post
It's not a security measure, but a matter of the shell not being able to find the script.

The PATH environment variable is what the shell looks at. If you were to copy your script into any of the directories that your path contains, it'll run just fine. The "./" merely states the explicit path, being where you are now. If your script was in /software/mytest/directory/myscript.pl, you could either type that entire thing in, or cd into that directory, and type in ./myscript.pl

With a Perl program, *MOST* of the time you put in a "#!/usr/bin/perl" (or wherever your Perl program is located), as the first line of the script. If you do a "chmod 755" on the .pl file, you can just run it by typing in the script name. If you don't have that as the first line, or need some explicit Perl run-time things, you can run it as "perl <script name>".
The following directory is in my $PATH variable /home/<user>/bin, so I have copied my script to here and ran the script from this directory. I now get a different error:
Quote:
/usr/bin/perl^M: bad interpreter: No such file or directory
So I think that the terminal is not liking my shebang (#!/usr/bin/perl)?

Also from experience a ^M anchor problem is some compatibility issue between Windows and Linux? My machine is dual boot so I wrote this script in Windows originally. However, I am using gvim as a text editor and cannot see any ^M anchors in my code when I open it up in Linux. This is very strange.
 
Old 03-15-2010, 02:47 AM   #10
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and Scientific Linux
Posts: 5,753

Rep: Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288
You can see the non printing chars by using cat with the -v flag:
Code:
cat -v hello_world.pl
Even if you don't see any, just to be sure you can run dos2unix or "flip -u" on the file.

Evo2.
 
1 members found this post helpful.
Old 03-15-2010, 02:55 AM   #11
sentez_mon_fromage
LQ Newbie
 
Registered: Mar 2010
Posts: 4

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by rockfall View Post
type export PATH=$PATH:. or system real hard use
Tried putting % export PATH=$PATH:. in the terminal and re-running. I still have the same problem.

OK, I have tried writing a brand new script, inside my /home/<user>/bin area. So I am writing this script on my Linux partition. It goes as follows:

Code:
#!usr/bin/perl
print"hello world\n";
I have saved it as "hello_world_2.pl". I have then run the following commands in my terminal:

% chmod 755 hello_world_2.pl
% ./hello_world_2.pl

I then get the following back ...

% bash: ./hello_world_2.pl: usr/bin/perl: bad interpreter: No such file or directory

If I re-run it with 'perl' fixed infront of it ...
% perl ./hello_world_2.pl
I get the expected 'hello world' response.

So there is no longer a ^M anchor being seen on the shebang but there is still something wrong with my bash set-up (I think) and the area of bash etc is still a very grey area for me.

Thanks
 
Old 03-15-2010, 02:57 AM   #12
mario.almeida
Member
 
Registered: May 2008
Location: India
Distribution: Ubuntu 10.04, CentOS
Posts: 179

Rep: Reputation: 27
Hi,

Your are missing -> /

Change this
#!usr/bin/perl

to this
#!/usr/bin/perl
 
Old 03-15-2010, 03:10 AM   #13
sentez_mon_fromage
LQ Newbie
 
Registered: Mar 2010
Posts: 4

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by evo2 View Post
You can see the non printing chars by using cat with the -v flag:
Code:
cat -v hello_world.pl
Even if you don't see any, just to be sure you can run dos2unix or "flip -u" on the file.

Evo2.
OK. Maybe my problems have something to do with my .vimrc set-up for my text editor?

The following appears along the bottom of my gvim editor:
Quote:
"hello_world.pl" [CR missing][dos] 8L, 182C
If I do your suggestion of "cat -v hello_world.pl" I see that every line of my script ends in ^M.

Once I run "dos2unix" on the file the script suddenly works! Thanks.

However, if I open up the file again in gvim, change it again and re-save it the file will not work. If I do a "cat -v" on it the ^Ms have re-appeared.

Re-running dos2unix solves this but there must be some reason as to why my text editor is adding the ^M anchors in the first place?
 
Old 03-15-2010, 03:14 AM   #14
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and Scientific Linux
Posts: 5,753

Rep: Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288
I don't really feel like participating in a round of editor wars: ... but you should be using emacs!

/me runs away

Evo2.
 
Old 03-15-2010, 04:10 AM   #15
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,260

Rep: Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328
Try just using the cmd line vim instead of gvim (after getting rid of the ^M ie dos2unix).
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Slackware 12.0 setup cannot find linux partitions to install too after fdisk setup . TDurden1937 Slackware - Installation 1 10-25-2008 09:18 PM
perl shell proxy setup Garp Linux - Networking 2 10-05-2005 06:23 PM
perl(Cwd) perl(File::Basename) perl(File::Copy) perl(strict)....What are those? Baldorg Linux - Software 1 11-09-2003 09:09 PM
Perl & "printer" setup joshb Linux - General 1 12-13-2002 05:50 PM


All times are GMT -5. The time now is 01:43 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration