Call Perl Scripts from Other Dir in Linux, Path Messed Up

P

Public Interest

I put my Perl script in a unix shell now. The filename is abc.pl

If I run "perl abc.pl", it works perfect
but if I run "perl /abs-path/abc.pl", it gave me the error file now found in
abc.pl. I opened several files in abc.pl. I think the perl script abc.pl use
the path veriable on the shell not where abc.pl sites. Because I want my
script to be portable, so I don't wnat to change the relative path within
abc.pl, so how do I tell abc.pl to set default path?

Anoher problem is that I run several scripts at once, and I put them in a
..sh file as
perl 111.pl
perl 222.pl
perl 333.pl
and I made the .sh file chmod 777, but when I type the file name RUN.sh, it
still tells me "command not found". I am not sure it is a shell problem or I
set up something wrong here.
 
J

Josef Möllers

Public Interest wrote:

First of all, try to get a decent name. I sincerely doubt your parents
called you "Public" ("Hey Pubby, it's dinnertime!")
I put my Perl script in a unix shell now. The filename is abc.pl

That won't work. Perl scripts belong in files not in unix shells.
If I run "perl abc.pl", it works perfect
but if I run "perl /abs-path/abc.pl", it gave me the error file now found in
abc.pl. I opened several files in abc.pl. I think the perl script abc.pl use

What files is abc.pl looking for and where are they? Although my crystal
ball is broken, somke shards still seem to work ...
I assume that your files reside in /abs-path and your current working
directory is not /abs-path. Then you might want to consult the manual
page of FindBin.
However, the use of this module is not a real solution. You might want
to consider other options like putting the path name(s) of the other
file(s) into an environment variable.
the path veriable on the shell not where abc.pl sites. Because I want my
script to be portable, so I don't wnat to change the relative path within
abc.pl, so how do I tell abc.pl to set default path?

Anoher problem is that I run several scripts at once, and I put them ina
.sh file as
perl 111.pl
perl 222.pl
perl 333.pl
and I made the .sh file chmod 777, but when I type the file name RUN.sh, it
still tells me "command not found". I am not sure it is a shell problemor I
set up something wrong here.

Try sh -x RUN.sh and it will tell you more.
Consulting another shard of my broken crystal ball, maybe the path of
perl is not in your PATH?

Josef
 
P

Public Interest

I followed your instruction as
sh -x RUN.sh
but except it prints something extra as
+ perl 111.pl
+ perl 222.pl
, it is the same as sh RUN.sh

I can run perl anywhere, so I think perl is in my path

in my abc.pl file, I "open (INPUT "input.txt")", so if the input file sits
right with the .pl file, and it works perfectly if I call from the same dir.
but not from other dirs

my file system:

/perlfiles/111.pl
/perlfiles/222.pl
/perlfiles/333.pl
/perlfiles/abc.pl
/perlfiles/input.txt
/perlfiles/RUN1.sh
/RUN2.sh

at /perlfiles/
"sh RUN1.sh" OK
"RUN1.sh" bash: RUN1.sh: command not found

at /
"sh /perlfiles/RUN1.sh" file input.txt not found in abc.pl
"sh RUN2.sh" file input.txt not found in abc.pl


What I think I can do is I CD to the path in RUN2.sh and run the script, and
CD back to the default dir. But it seems other better ways to do it.
 
J

Josef Möllers

Public said:
I followed your instruction as
sh -x RUN.sh
but except it prints something extra as
+ perl 111.pl
+ perl 222.pl
, it is the same as sh RUN.sh

I can run perl anywhere, so I think perl is in my path

in my abc.pl file, I "open (INPUT "input.txt")", so if the input file sits
right with the .pl file, and it works perfectly if I call from the samedir.
but not from other dirs

That's what the notions of "current working directory" and "relative
path" do.
You either specify an absolute path or your specify a relative path and
the system sort-of prepends the current working directory.
my file system:

/perlfiles/111.pl
/perlfiles/222.pl
/perlfiles/333.pl
/perlfiles/abc.pl
/perlfiles/input.txt
/perlfiles/RUN1.sh
/RUN2.sh

at /perlfiles/
"sh RUN1.sh" OK
"RUN1.sh" bash: RUN1.sh: command not found
at /
"sh /perlfiles/RUN1.sh" file input.txt not found in abc.pl
"sh RUN2.sh" file input.txt not found in abc.pl

What I think I can do is I CD to the path in RUN2.sh and run the script, and
CD back to the default dir. But it seems other better ways to do it.

As for finding executables: add their absolute path(s) to the PATH
environment variable.

As for finding files: either put them into a well-known location (/lib,
/usr/lib, $HOME/lib, ...) or provide the absolute path of their location
into an environment variable (MY_DATAFILE_PATH=/perlfiles) or into a
configuration file ($HOME/.myperlconfig) or whatever, but the system is
pretty helpless in guessing where your files are.
 
S

Steve Grazzini

In comp.lang.perl.misc Public Interest said:
in my abc.pl file, I "open (INPUT "input.txt")", so if the input
file sits right with the .pl file, and it works perfectly if I
call from the same dir. but not from other dirs

Then you need to use absolute paths -- or paths relative to the
location of your script -- with open().

use FindBin qw($Bin);
open INPUT, "$Bin/input.txt" or die "open: $Bin/input.txt: $!";

[ f'ups to clpm ]
 
B

Ben Morrow

Public Interest said:
in my abc.pl file, I "open (INPUT "input.txt")", so if the input file sits
right with the .pl file, and it works perfectly if I call from the same dir.
but not from other dirs

You want to put
chdir '/perlfiles';
or so at the start of your Perl script.
my file system:

/perlfiles/111.pl
/perlfiles/222.pl
/perlfiles/333.pl
/perlfiles/abc.pl
/perlfiles/input.txt
/perlfiles/RUN1.sh
/RUN2.sh

at /perlfiles/
"sh RUN1.sh" OK
"RUN1.sh" bash: RUN1.sh: command not found

Unlike DOS, Unix shells do not by default (nowadays) have '.' (the
current working directory) in your PATH. This means that to run
RUN1.sh you need to invoke it as './RUN1.sh'. Or you can find what
your PATH is (echo $PATH) and copy RUN1.sh into one of those
directories.

Also, [OT], you don't really want to be stuffing things into / like
that... at least put them under /usr/local. And it is not conventional
under Unix to use 'file extensions' for executable script files.

Ben
 
B

Bart Lateur

Public said:
I can run perl anywhere, so I think perl is in my path

That's not your problem. your problem is here:

I'm sure you're using relative paths? Then your problem is simply in
your current directory when the script is run.

You could demand that people chdir to the path containing the files,
before calling your script, or you could use the FindBin module to find
out where the script itself is. I'll stay a bit sketchy on that
approach, because FindBin is somewhat broken, it sometimes refuses to
work in mysterious ways.

The approach I'm advocating, is to simply figure out the path, either
absolute or relative, out of $0, the name for the script, since that is
*always* a valid path, at startup. At least, I can't think of any
counter-examples.

So I guess this should work, at least on Unixy systems:

$0 =~ /(.*)\// and chdir $1;

Now your script should be able to find the files.
 
B

Ben Morrow

Bart Lateur said:
You could demand that people chdir to the path containing the files,
before calling your script, or you could use the FindBin module to find
out where the script itself is. I'll stay a bit sketchy on that
approach, because FindBin is somewhat broken, it sometimes refuses to
work in mysterious ways.

The approach I'm advocating, is to simply figure out the path,
either absolute or relative, out of $0, the name for the script,
since that is *always* a valid path, at startup. At least, I can't
think of any counter-examples.

You should check what you write before you post. For instance, have a
look at perldoc -m FindBin. What FindBin does is make a rather more
sophisticated attempt than you recommend at extracting the path of the
script out of $0: the fact that it sometimes refuses to work is
because this is not always possible. For instance, invoking a script
as 'perl script' will leave only 'script' in $0, with no path info at
all. See also KNOWN BUGS in perldoc FindBin: there is no way round
this problem.

Ben
 
P

Public Interest

You want to put
chdir '/perlfiles';
or so at the start of your Perl script.

Another problem is Perl Script under Shell and under CGI use different dir
systems. How can I make sure that both will work properly?

/ under shell means root dir
/ under cgi or http means http://domain.com/
sometimes. cgi use a cgi-bin dir which is virtual so domain.com/cgi-bin and
domain.com/htmlfiles are not under the same root but rather
/www/html/htmlfiles/ and www/cgi-bin.

the only thing that works in both is ./ ../

What I think of is still use relative path for portabilty of scripts under
shell and web server. I want to add something to change pwd enviroment, but
not sure if it is possible. PWD is not accessable under CGI. CGI has an env
as script_path or scrip_url.
 
J

Jürgen Exner

Public said:
Another problem is Perl Script under Shell and under CGI use
different dir systems. How can I make sure that both will work
properly?

/ under shell means root dir
/ under cgi or http means http://domain.com/

Dear public

That is BS. You are confusing URLs (or URIs) with file names resp. directory
pathes.
The one has nothing to do with the other and it is only for convenience that
many web servers try to interpret a URL path as a directory path of a file
system. However it is really up to the web server how to interpret any URL
and what to return in each case. In fact very often the "target HTML file"
doesn't even exist but is created on the fly by some script or pulled out of
a database or publishing system or a gazillion of other sources.

jue
 
B

Bart Lateur

Ben said:
You should check what you write before you post. For instance, have a
look at perldoc -m FindBin. What FindBin does is make a rather more
sophisticated attempt than you recommend at extracting the path of the
script out of $0: the fact that it sometimes refuses to work is
because this is not always possible.

Let me point you to this node:

FindBin is broken (RE: How do I get the full path to the script
executing?)
<http://perlmonks.org/index.pl?node_id=41213>

Points:

- FindBin does a useless search. There is *no need* to scan $PATH, as
perl scripts aren't ever searched in it.
- It sometimes fails because it can't have access to directories it
wants to look into. Even though the original path works, thank you very
much, FindBin prefers to returns an empty string (or undef?), which
doesn't work.
 
B

Ben Morrow

Bart Lateur said:
Ben Morrow wrote:
Let me point you to this node:

FindBin is broken (RE: How do I get the full path to the script
executing?)
<http://perlmonks.org/index.pl?node_id=41213>

The final comment on that post is pertinent... setid scripts ought to
only get a /dev/fd argument for the script, in which case the task is
impossible... interestingly, mine seem not no. Worrying...
Points:

- FindBin does a useless search. There is *no need* to scan $PATH, as
perl scripts aren't ever searched in it.

What's the -S option for then? No, OK, in that case perl knows the
full path and can put in $0.
- It sometimes fails because it can't have access to directories it
wants to look into. Even though the original path works, thank you very
much, FindBin prefers to returns an empty string (or undef?), which
doesn't work.

OK, FindBin is broken. Let's work out what's wrong, and fix it. :)

Ben
 
C

Chris

Public Interest wrote:
[snip]

Firstly, this post is OT. It has nothing to do with linux. So why are
you x-posting here. The other ng you should consider is a shell one.

Secondly, it strikes me that your grasp of both perl and shell scripting
is very limited (hence why you're posting a question, I guess). Your
post raises many unanswered questions:
1) Why are using a shell script to run several perl scripts when you
should really be combining them all into one perl script?
2) Why aren't you using a shebang line (#!) at the top of your perl
script to tell the shell where to run perl from thus avoiding 'perl
script.pl'?
3) Why don't you post the code to your perl scripts, that way we won't
have to guess what's wrong with them?
4) In your later posts you mention CGI. Why are you even thinking about
CGI when you still haven't sorted out your simple perl/shell problem(s)?

Go back. Rethink your questions. READ THE DOCS/FAQS/BOOKS. Search
google. Only then post a question (with relevent code) to a relvent ng.
 
S

Steve Grazzini

In comp.lang.perl.misc Bart Lateur said:
- FindBin does a useless search. There is *no need* to scan $PATH, as
perl scripts aren't ever searched in it.

One of the authors said on p5p (http://xrl.us/2gb)

On some platforms when a script is called via PATH, instead of
directly, $0 does not contain the full path to the script (I am
assuming that caller would return the same value as $0 in such
a case). This is why FindBin goes to a great extent to locate
the real position of the script.

But I'm not sure whether this is correct. It looks to me like the
only time perl searches $PATH is when running under the -S switch,
regardless of platform. And when perl finds the script in $PATH, it
will always set $0 to an absolute path or a path relative to the
current directory.

Getting rid of the $PATH search would also fix the "KNOWN BUG" in
FindBin's documentation.
- It sometimes fails because it can't have access to directories it
wants to look into. Even though the original path works, thank you very
much, FindBin prefers to returns an empty string (or undef?), which
doesn't work.

Do you know if either of these have been reported as bugs?

I found this ticket (from 1999, marked "resolved") but couldn't find
the explanation.

http://rt.perl.org/rt2/Ticket/Display.html?id=1845
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top