witty
asked on
Get fullpath of script
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
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
$fullpath = $cwd/$0;
I forgot the quotes; it should read
$fullpath = "$cwd/$0"; # unix
$fullpath = "$cwd\$0"; # windows
I forgot the quotes; it should read
$fullpath = "$cwd/$0"; # unix
$fullpath = "$cwd\$0"; # windows
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.
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
#!/usr/bin/perl
print `pwd`;
Using the shell command pwd? Works on older versions of Perl as well:)
print `pwd`;
Using the shell command pwd? Works on older versions of Perl as well:)
ASKER
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
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
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.
oOOOOOPPPSSSS..... - was just offering my thoughts - and it looks like they weren't exactly worth the 2p offered :)
$cwd = cwd();
$fullpath = $cwd/$0;