Solved

Get fullpath of script

Posted on 2003-11-10
11
761 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
  • 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
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…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

920 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now