Running perl scripts 
Author Message
 Running perl scripts

This is a stupid question, I am quite sure.
Say I just wrote up a perl script, hello.pl. From my unix shell
interface, how do I run the script? I've searched all over the
internet for an answer to this seemingly simple question, I just can't
seem to figure it out.

The best I can seem to do is to type perl, hit enter, paste the
script, hit control-d and then it works. Obviously, there has got to
be run a saved perl script, right?

So, I'm in my home directory where the file is saved.
I type:
%hello.pl
I get the responce:
hello.pl: Command not found.

I saw somewhere that you have to chmod +x it, but that didn't help at
all.
What haven't I done?

Please flame me if it will make your day any better.

Katan8472



Mon, 01 Mar 2004 20:58:32 GMT  
 Running perl scripts
Yes you need to 'chmod +x hello.pl' and then from the directory where the
hello.pl is located do a './hello.pl' or 'perl hello.pl' (w/out quotes).

-Jayson Garrell


Quote:
> This is a stupid question, I am quite sure.
> Say I just wrote up a perl script, hello.pl. From my unix shell
> interface, how do I run the script? I've searched all over the
> internet for an answer to this seemingly simple question, I just can't
> seem to figure it out.

> The best I can seem to do is to type perl, hit enter, paste the
> script, hit control-d and then it works. Obviously, there has got to
> be run a saved perl script, right?

> So, I'm in my home directory where the file is saved.
> I type:
> %hello.pl
> I get the responce:
> hello.pl: Command not found.

> I saw somewhere that you have to chmod +x it, but that didn't help at
> all.
> What haven't I done?

> Please flame me if it will make your day any better.

> Katan8472



Mon, 01 Mar 2004 21:08:16 GMT  
 Running perl scripts

Quote:

> This is a stupid question, I am quite sure.

It's a novice question. It's also a *nix question, not a Perl one. We'll
let the off-topicness slide this time. :)

Quote:
> Say I just wrote up a perl script, hello.pl. From my unix shell
> interface, how do I run the script?
> [...]
> The best I can seem to do is to type perl, hit enter, paste the
> script, hit control-d and then it works. Obviously, there has got to
> be run a saved perl script, right?

Of course. :)

Quote:
> So, I'm in my home directory where the file is saved.
> I type:
> %hello.pl
> I get the responce:
> hello.pl: Command not found.

That's a response from your shell. It means that the program 'hello.pl'
was not found in your path. The current directory '.' is usually not in
your path by default because it's a potential security problem. You can
do one of three things:

  1) run scripts by passing them to perl:
     % perl hello.pl
  2) Specify the path to the script:
     % ./hello.pl
  3) Add '.' to your path:
     % hello.pl

If you do (3), make sure to make '.' the *last* thing in your path.

Quote:
> I saw somewhere that you have to chmod +x it, but that didn't help at
> all.

It did help, if you want to be able to run the script without passing it
to perl as in (1). You just haven't gotten to the part where it matters
yet.

Quote:
> Please flame me if it will make your day any better.

Not particularly.

-mjc



Mon, 01 Mar 2004 22:31:30 GMT  
 Running perl scripts

Quote:


>> Say I just wrote up a perl script, hello.pl. From my unix shell
>> interface, how do I run the script?

                       ^^^

There is some spotty information ('cause it depends on the
operating system, not the programming language) in the
obscurely named:

   perldoc perlrun

:-)

Quote:
>You can
>do one of three things:

>  1) run scripts by passing them to perl:
>     % perl hello.pl
>  2) Specify the path to the script:
>     % ./hello.pl
>  3) Add '.' to your path:
>     % hello.pl

Note that if you do 2 or 3, then you also need a proper "shebang"
line. I'm not sure the OP knows what that is. You need to put:

#!/usr/bin/perl -w

as _the very first line_ in the file. You may have to modify
that if your perl is not installed in /usr/bin.

Quote:
>> Please flame me if it will make your day any better.

Your feet stink!

Thanks a lot. I feel better now.

--
    Tad McClellan                          SGML consulting

    Fort Worth, Texas



Mon, 01 Mar 2004 23:57:10 GMT  
 Running perl scripts


Quote:

>> This is a stupid question, I am quite sure.

>It's a novice question. It's also a *nix question, not a Perl one. We'll
>let the off-topicness slide this time. :)

>> So, I'm in my home directory where the file is saved.
>> I type:
>> %hello.pl
>> I get the responce:
>> hello.pl: Command not found.

>That's a response from your shell. It means that the program 'hello.pl'
>was not found in your path. The current directory '.' is usually not in
>your path by default because it's a potential security problem. You can
>do one of three things:

It can also mean that your script starts with a #! line that doesn't
have a valid path to perl.

For example, if your script starts off "#! /usr/local/bin/perl", but
perl is /usr/bin/perl on that system, you may see this message.

Occasionally, this results from a script being moved from Windows to
Unix in such a way that the ^M is not removed from the end of the line.

You can fix this by:

a)  Running your script through "dos2unix", or otherwise removing the ^M chars.
b)  Adding -w to the end of the #! line, if it isn't there already.
c)  Both of the above.

The best answer in this case is c).

-- Gary Ansok



Tue, 02 Mar 2004 00:05:57 GMT  
 Running perl scripts

Quote:



>>> So, I'm in my home directory where the file is saved.
>>> I type:
>>> %hello.pl
>>> I get the responce:
>>> hello.pl: Command not found.

>>That's a response from your shell. It means that the program 'hello.pl'
>>was not found in your path. The current directory '.' is usually not in
>>your path by default because it's a potential security problem. You can
>>do one of three things:

> It can also mean that your script starts with a #! line that doesn't
> have a valid path to perl.

> For example, if your script starts off "#! /usr/local/bin/perl", but
> perl is /usr/bin/perl on that system, you may see this message.

This seems to be a function of what shell you're using:

    $ cat try.it
    #!/no/such/directory/perl -w
    exit 0;
    $ chmod +x try.it
    $ ./try.it
    zsh: no such file or directory: ./try.it
    $ try.it
    zsh: command not found: try.it
    $ ksh
    $ ./try.it
    ksh: ./try.it:  not found
    $ try.it
    ksh: try.it:  not found
    $ ^D
    $ csh
    % ./try.it
    ./try.it: Command not found
    % try.it
    try.it: Command not found
    $ bash
    $ ./try.it
    bash: ./try.it: No such file or directory
    $ try.it
    bash: try.it: command not found
    $ exit
    $ sh
    $ ./try.it
    ./try.it: not found
    $ try.it
    try.it: not found
    $

It seems that zsh and bash *do* make a distinction between the
interpreter not found (#!) and the command not found.  But my versions
of sh, ksh and csh do not distinguish between these two.  

--
Garry Williams



Tue, 02 Mar 2004 01:09:41 GMT  
 Running perl scripts
Thanks all for help, I actually found out what was wrong. Since I've
been using a non-unix computer for so long now it never actually
accured to me that a perl script might not be a text file. I'm using
Mac OSX, so when I went into my terminal and tried to run hello.pl
nothing happened. When I did 'cat >' to the same script it worked
perfectly.

Even though I had everything set up right accept for my file type, I
learned a lot about running perl scripts in unix from you guys, now if
something real actually goes wrong I'm confident I'll know how to fix
it.

If anyone knows how to make BBEdit create files that are executable by
unix in the command line that would be helpful at this point.

Katan8472

Quote:



> >> Say I just wrote up a perl script, hello.pl. From my unix shell
> >> interface, how do I run the script?
>                        ^^^

> There is some spotty information ('cause it depends on the
> operating system, not the programming language) in the
> obscurely named:

>    perldoc perlrun

> :-)

> >You can
> >do one of three things:

> >  1) run scripts by passing them to perl:
> >     % perl hello.pl
> >  2) Specify the path to the script:
> >     % ./hello.pl
> >  3) Add '.' to your path:
> >     % hello.pl

> Note that if you do 2 or 3, then you also need a proper "shebang"
> line. I'm not sure the OP knows what that is. You need to put:

> #!/usr/bin/perl -w

> as _the very first line_ in the file. You may have to modify
> that if your perl is not installed in /usr/bin.

> >> Please flame me if it will make your day any better.

> Your feet stink!

> Thanks a lot. I feel better now.



Tue, 02 Mar 2004 05:39:53 GMT  
 Running perl scripts
On Thu, 13 Sep 2001 16:31:30 -0500,

Quote:
>   3) Add '.' to your path:
>      % hello.pl

> If you do (3), make sure to make '.' the *last* thing in your path.

But it would be much, much better to not add it at all. Just type

        ./hello.pl

Much safer that way. and ./ isn't that hard to type.

Martien

PS. Imagine someone creates a script called xs in /tmp, that executes

rm -rf /

One day, you'll end up in /tmp, and your fingers slip when you want to
type cd, and you'll need last night's backup tapes, and have earned
the eternal wrath of your system admin. And yes, I type xs instead of
cd reasonably often. There are other 'standard' typos of commands.
--
Martien Verbruggen              |
Interactive Media Division      | You can't have everything, where
Commercial Dynamics Pty. Ltd.   | would you put it?
NSW, Australia                  |



Tue, 02 Mar 2004 05:54:25 GMT  
 Running perl scripts

Quote:

>On Thu, 13 Sep 2001 16:31:30 -0500,


[snip cwd in path]

[snip bad 'xs' program waiting for 'cd' to be mistyped]

Quote:
>There are other 'standard' typos of commands.

Do you know where there is a list of them? I'd like to scan the fs
for such names from time to time.

I have:

   alias gerp='/bin/grep'
   alias sl='/bin/ls'

in some of my startup config files.

P.S. For some strange reason I like pronouncing "gerp". It's not
     in the same class as "salsa", but it's nice anyway :-)

--
    Tad McClellan                          SGML consulting

    Fort Worth, Texas



Tue, 02 Mar 2004 16:18:01 GMT  
 Running perl scripts
On Fri, 14 Sep 2001 15:18:01 GMT,

Quote:

>>On Thu, 13 Sep 2001 16:31:30 -0500,

> [snip cwd in path]

> [snip bad 'xs' program waiting for 'cd' to be mistyped]

>>There are other 'standard' typos of commands.

> Do you know where there is a list of them? I'd like to scan the fs
> for such names from time to time.

I haven't ever seen such a list, and a bit of google searching doesn't
turn up much either. I didn't mean 'standard' as in well-documented,
really, more as something that many people mistype. I know the subject
comes up regularly on comp.unix.shells and comp.unix.programmer, and
sometimes people post examples.

Quote:
> I have:

>    alias gerp='/bin/grep'
>    alias sl='/bin/ls'

The sl one is one that I see a lot as well on my command line. Others
are

dc          (cd, but this one won't hurt with . at the end of the path)
fidn        (instead of find)
elss        (instead of less)
an          (instead of man)
tzr         (instead of tar)
csv         (instead of cvs)
gvi         (instead of gvim)

Any command where a 'left-hand' and a 'right-hand' letter follow each
other is subject to having those two reversed, the most frequently used
commands of course being the most frequently mistyped. Commands
containing a 'pinky' letter or some letters from the bottom row are
vulnerable for some typists (me, for example). Sometimes the first
letter of a command gets swallowed because the terminal is in an odd
state, for example after accidentally cat-ing a binary file.

Then there's a set where a space is missing:

ls-al (and other option combinations)
cd-
cd..
pushd-1 or pu-1 (which is why I have aliases 1,2,3 etc for these)
tarxvf (and other options)
df-k
du-sk

And I'm sure there are more that occur less frequently, which I can't
readily think of right now. On my laptop there another set of potential
problems, because of the odd arrangement of the keys, and I sometimes
end up accidentally using ctrl-a in the middle of typing a comand.

If you're going to look for these things, you should probably consider
using the locate database. It can severely shorten the time needed to
find offending file names. :)

I have on some occasions also found traps for sudo and su (and often
su-), which are of course particulary dangerous (capture password, then
exec real program). Anyone who ever needs to use su or sudo, or work
with non-default privileges, should be particularly careful not _ever_
to have . in their path.

Just for fun, I just went through the history in a few of my open
terminals.  I could only find 2 of the ones above (tzr and an), but I've
got a distint memory of typing some of the others at some time in the
past.

Martien
--
Martien Verbruggen              |
Interactive Media Division      | life ain't fair, but the root
Commercial Dynamics Pty. Ltd.   | password helps. -- BOFH
NSW, Australia                  |



Wed, 03 Mar 2004 02:11:59 GMT  
 
 [ 10 post ] 

 Relevant Pages 

1. Problems running perl scripts in perl scripts! HELP

2. How: Run Perl Script Within Perl Script !!

3. Running perl scripts inside perl scripts (TIA)

4. Run Perl script from within Perl script?

5. Running Perl script on remote machine and returning result to script on current machine

6. Running Perl script w/i Unix Shell Script

7. Help: Running Perl script from Shell script, passing variables

8. how to run Perl script on remote server not having Perl interpretor

9. running perl script from perl

10. mod_perl apache windows 2000 is not running perl scripts simultaneously

11. Editing a running perl script

12. Running perl scripts on NT as a Service

 

 
Powered by phpBB® Forum Software