Solved

Get fullpath of script

Posted on 2003-11-10
11
769 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
ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 
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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Extract multiple value with delimiters from a string 4 170
SIMPLE Perl Regex 1 159
File Find regex problem 4 62
Internal Server Error on 1&1 Apache Server running PERL 3 21
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…
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
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…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

803 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