Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

find scripts directory location

Posted on 2006-06-09
4
Medium Priority
?
241 Views
Last Modified: 2012-05-05

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.


0
Comment
Question by:Marketing_Insists
  • 2
4 Comments
 
LVL 6

Accepted Solution

by:
GnarOlak earned 1200 total points
ID: 16872858
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
 

Author Comment

by:Marketing_Insists
ID: 16872937
Yes, that's it exactly, thanks!
0
 
LVL 6

Expert Comment

by:tone28
ID: 16872997
I would probably

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

pop(@parts);

print join "\\",@parts;
0
 
LVL 6

Expert Comment

by:tone28
ID: 16873007
oops too late.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
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…
Six Sigma Control Plans

876 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