- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- Re: Perl: Current Directory
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-07-2005 06:57 PM
тАО11-07-2005 06:57 PM
I was using a piece of code to find out the curent dir of the script:
use Cwd;
my $dir = getcwd; #current directory
I found something special: When the script , let's say in /tmp/scripts has been run by another script in /tmp, then $dir contains the current directory of the "main" script (/tmp), and not the one I need (/tmp/scripts).
Does anybody know another function that works properly?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-07-2005 07:03 PM
тАО11-07-2005 07:03 PM
SolutionIn child script change directory with cd then print getcwd information. It will show /tmp/scripts locations.
hth.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-07-2005 07:06 PM
тАО11-07-2005 07:06 PM
Re: Perl: Current Directory
You can also see this behaviour if you go in a different directory and invoke the script using the full path
ie.
#cd /
#/tmp/scripts/myscript
then cwd will be / not /tmp/scripts
hth
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-07-2005 07:09 PM
тАО11-07-2005 07:09 PM
Re: Perl: Current Directory
-Arun
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-07-2005 07:43 PM
тАО11-07-2005 07:43 PM
Re: Perl: Current Directory
Imagine, I want to put the whole stuff on another server and the main script is not in /tmp, but in /usr. This is why I cannot use absolute names. I need for each script to find out where it resides and put that in $dir
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-07-2005 07:55 PM
тАО11-07-2005 07:55 PM
Re: Perl: Current Directory
use FindBin;
to find the directory where the perl script resides.
http://perl.org.il/pipermail/perl/2003-January/000948.html
-Arun
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-07-2005 09:25 PM
тАО11-07-2005 09:25 PM
Re: Perl: Current Directory
2. check the scripts for chdir () calls. The script, whoever/whatever called it, can be in one single location at the time, so if a parent script (sh, perl, C, ...) does a cd/chdir, the child script shares that location
3. If you need to know the location of the running script, you can indeed use FindBin, but you can also examine $0 and/or $^X
Demo:
lt09:/tmp 110 > cat test.sh
#!/bin/sh
pwd
echo none
perl -l test.pl
echo relative
perl -l ./test.pl
echo absolute
perl -l /tmp/test.pl
echo chdir
cd /usr
pwd
echo none
test.pl
echo relative
../tmp/test.pl
echo absolute
/tmp/test.pl
echo extend PATH
export PATH=$PATH":/tmp"
echo none
test.pl
echo relative
../tmp/test.pl
echo absolute
/tmp/test.pl
lt09:/tmp 111 > cat test.pl
#!/pro/bin/perl
use warnings;
use strict;
use Cwd;
my $pwd = getcwd;
print "\$0: $0, \$^X: $^X, pwd: $pwd\n";
lt09:/tmp 112 > sh test.sh
/tmp
none
$0: test.pl, $^X: /pro/bin/perl, pwd: /tmp
relative
$0: ./test.pl, $^X: /pro/bin/perl, pwd: /tmp
absolute
$0: /tmp/test.pl, $^X: /pro/bin/perl, pwd: /tmp
chdir
/usr
none
test.sh: line 19: test.pl: command not found
relative
$0: ../tmp/test.pl, $^X: /pro/bin/perl, pwd: /usr
absolute
$0: /tmp/test.pl, $^X: /pro/bin/perl, pwd: /usr
extend PATH
none
$0: /tmp/test.pl, $^X: /pro/bin/perl, pwd: /usr
relative
$0: ../tmp/test.pl, $^X: /pro/bin/perl, pwd: /usr
absolute
$0: /tmp/test.pl, $^X: /pro/bin/perl, pwd: /usr
lt09:/tmp 113 >
Enjoy, Have FUN! H.Merijn