Solved

Get fullpath of script

Posted on 2003-11-10
11
750 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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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 …
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

762 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

19 Experts available now in Live!

Get 1:1 Help Now