Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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?
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.
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>".
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.
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).
Firstly, I am overwhelmed by the number of responses here ...
Quote:
Originally Posted by Tinkster
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
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
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.
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.
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?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.