find scripts directory location


If $0 gives the location of the script you are running, how can I just get the directory where the script is located?

CWD; doesn't work the I want, since if I execute a script located on a network drive from the commandline on my local PC, the working directory would be whatever directory I'm in at my shell.


Marketing_InsistsAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
GnarOlakConnect With a Mentor Commented:
If what you are asking is how to extract from $0, which has both a path and the name of the script, just the directory then you can use File::Basename thus:

use File::Basename;

$script = basename ($0);
$dir = dirname($0);

print "$script was run from $dir\n";
__END__

If that isn't what you need then please elaborate.

0
 
Marketing_InsistsAuthor Commented:
Yes, that's it exactly, thanks!
0
 
tone28Commented:
I would probably

my @parts = split('\\\\',$0);

pop(@parts);

print join "\\",@parts;
0
 
tone28Commented:
oops too late.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.