Solved

Get fullpath of script

Posted on 2003-11-10
11
789 Views
Last Modified: 2012-06-21
Hi!

Does anyone know, how to get the full-path of the directory of the perl-script!

This should work from ksh and bash!
my workaround:
---cut---
my $fullpath;

if ( $0=~/(^\/.*?)[^\/]*$/ ){
  #if called with fullpath or if script is in a ENV{PATH}-directory
  $fullpath=$1;
}else{
  $0=~/(.*?)[^\/]*$/;
  $fullpath=$ENV{PWD}."/".$1;
}
---cut---

but I guess, there must be a more simple solution!!!

thx
michi
0
Comment
Question by:witty
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +2
11 Comments
 
LVL 28

Expert Comment

by:FishMonger
ID: 9713506
Use Cwd;

$cwd = cwd();
$fullpath = $cwd/$0;
0
 
LVL 28

Expert Comment

by:FishMonger
ID: 9713525
$fullpath = $cwd/$0;

I forgot the quotes; it should read

$fullpath = "$cwd/$0";  # unix
$fullpath = "$cwd\$0";  # windows
0
 
LVL 20

Expert Comment

by:jmcg
ID: 9715568
Actually, it is not always possible to discover the pathname of the script.

You have to start with $0, that's certain.

If $0 contains a / it is most likely a path relative to the current working directory -- but it may contain .. which will make simple concatenation yield a messy-looking path. There's help for this:

use Cwd qw(abs_path);

$fullpath = abs_path( $0) unless $0 =~ m{/};

Otherwise, you have to see if the name given in $0 appears as a directory entry in one of the elements of $ENV{PATH}.

@dirs = split /:/, $ENV{PATH};
my $path_found;
for (@dirs) {
     do { $path_found = $_/$0; last; } if -x $_/$0 ;
     }

and that, too, may need to be reformed:

$fullpath = abs_path($path_found);

Putting all of that together....

use Cwd qw(abs_path);

our $fullpath;
my $path_found;
if( $0 =~ m{/} ) {
     $path_found = $0;
   } else {
     for (split /:/, $ENV{PATH}) {
          do { $path_found = $_/$0; last } if -x $_/$0;
         }
    }

if ( $path_found ) {
   $full_path = abs_path( $path_found);
   } else {
   warn "could not locate full pathname for script $0";
   }

That will cover most possibilities, but it's still possible to execute something along a path that contains a directory you cannot read, which is one of the ways for it to be impossible to discover the proper name of the script.

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 48

Accepted Solution

by:
Tintin earned 63 total points
ID: 9718463
In almost all cases, the following will work:

#/usr/bin/perl
use FindBin qw($Bin $RealBin);

print "The directory this script lives in is $Bin\n";
print "The directory this script lives in with any links resolved is $RealBin\n";
0
 
LVL 20

Assisted Solution

by:jmcg
jmcg earned 62 total points
ID: 9718843
Agreed. I second the suggestion to use FindBin (now a standard part of Perl).

 I looked at the source for FindBin and recognized all the portability considerations that it contains that my offering did not. The underlying method is identical, though, so I was on the right track.

0
 
LVL 22

Expert Comment

by:pjedmond
ID: 9727698
#!/usr/bin/perl

print `pwd`;


Using the shell command pwd? Works on older versions of Perl as well:)
0
 
LVL 1

Author Comment

by:witty
ID: 9733710
pwd doesn't have to be the script's directory!!!

you can call the perl-script with:

from everywhere:
/home/user/perl/script.pl

from "/home/user/x/y/":
../../perl/script.pl

from "/home/user/perl/"
script.pl
or
./script.pl

an if "/home/user/perl/" is included in $PATH:
from everywhere:
script.pl

michi
0
 
LVL 20

Expert Comment

by:jmcg
ID: 9733860
Yes, what you say is quite true. Some of the offered answers have not taken that into account. But the code I offered and the code in the FindBin module do take it into account and should give you the answer you asked for.
0
 
LVL 22

Expert Comment

by:pjedmond
ID: 9735662
oOOOOOPPPSSSS..... - was just offering my thoughts - and it looks like they weren't exactly worth the 2p offered :)
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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.…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

624 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