Solved

Get fullpath of script

Posted on 2003-11-10
11
774 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
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…

830 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