Re: chdir in perl
by jethro (Monsignor) on Sep 25, 2009 at 12:16 UTC
|
Using the backticks creates a subprocess and executes the shell command chdir (which probably doesn't exist) there, which doesn't change the environment in the process that is running your script
the perl function chdir() works as intended. Check if the path in $mpath really exists and you have the rights to chdir to it
UPDATED three times. Sloppy writing and code reading by me
| [reply] |
Re: chdir in perl
by merlyn (Sage) on Sep 25, 2009 at 14:28 UTC
|
$mpath = `/vobs/package134/inttools/modules/unixtools/srce/whichPat
+h $m`;
...
if(! chdir($mpath)){
I'm betting there's a newline on the end of $mpath. Since newline is a valid character within (and therefore at the end of) a path, the chdir() function cannot ignore it, and will look for a path that ends in newline, which you probably don't have.
Consider a chomp on $mpath.
-- Randal L. Schwartz, Perl hacker
The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this document are to be interpreted as described in RFC 2119.
| [reply] [d/l] [select] |
|
|
I'm betting there's a newline on the end of $mpath. Since newline is a valid character within (and therefore at the end of) a path, the chdir() function cannot ignore it, and will look for a path that ends in newline, which you probably don't have.
Consider a chomp on $mpath.
Great catch! You just solved an hour-long headscratching for me!
| [reply] |
Re: chdir in perl
by tbone1 (Monsignor) on Sep 25, 2009 at 12:33 UTC
|
Okay, maybe I'm misreading this, BUT:
It seems as if you are doing a `chdir $mpath`; which shells out to change the directory to the value of $mpath. Then you try to a chdir($mpath) ... which seems redundant.
One thing: are all the values of $mpath ones that would work fine on a chdir? Meaning, no spaces, no tabs, etc? Just a though, because I've been bitten by that myself.
--
tbone1, YAPS (Yet Another Perl Schlub)
And remember, if he succeeds, so what.
- Chick McGee
| [reply] |
Re: chdir in perl
by repellent (Priest) on Sep 25, 2009 at 15:45 UTC
|
use Cwd qw(chdir);
to keep $ENV{PWD} updated as you chdir(). | [reply] [d/l] [select] |
|
|
Dear All,
Thanks for your concerns and suggestions.
The idea of $mpath i.e., when I execute this command: `/vobs/package134/inttools/modules/unixtools/srce/whichPath $m :: I will get the output as /vobs/package183/psappat/modules/<module name, that is having the $m value in the foreach loop>, So now I want to cd into this module, and then cd into confm folder, Here in this path/location, My intention is to check whether post_REFERENCED_to_EXPERIMENTAL file exists? and if so, find the link attached to this file. My concern is still I am not able to CHDIR into module. Please suggest.
Modified Script:
use Cwd qw(chdir);
open(LS, "/home/raghvens/52xxmodulelist.txt") or die $!;
@modlist = <LS>;
@mpath = " ";
foreach $m (@modlist)
{
print "\nProcessing $m .. \n";
$mpath = `/vobs/package134/inttools/modules/unixtools/srce/whichPat
+h $m`;
print "\nModule Path is $mpath \n";
chomp($mpath);
$path = `chdir $mpath`; # or die "Can't cd into module path:$mpath
+\n";
print "\nCurrent Path is $path";
if(! chdir($mpath)){
print "\n cannot change the working directory \n";
}
`chdir confm`;
$file = "post_REFERENCED_to_EXPERIMENTAL";
@output = system ("ls -l $file");
print "YES, Link is present" if $_ =~ /\-\>/ ;
}
This is the output of the script:
Processing atp
..
Module Path is /vobs/package183/psappat/modules/atp
post_REFERENCED_to_EXPERIMENTAL: No such file or directory
Current Path is
Processing atpctm
..
Module Path is /vobs/package183/psappat/modules/atpctm
post_REFERENCED_to_EXPERIMENTAL: No such file or directory
| [reply] [d/l] [select] |
|
|
Why do you think chdir is not working?
Make sure you understand the previous responses. They all provide good advice and relevant information. To re-iterate: you should use the chdir function rather than executing the chdir command in a sub-process.
But I wouldn't use chdir myself. I would do something more like the following:
use strict;
use warnings;
open( my $fh, "/home/raghvens/52xxmodulelist.txt" ) or die $!;
foreach my $m (<$fh>) {
chomp($m);
print "\nProcessing \"$m\" .. \n";
my $path = `/vobs/package134/inttools/modules/unixtools/srce/which
+Path $m`;
chomp($path);
$path .= '/confm/post_REFERENCED_to_EXPERIMENTAL';
print "\tChecking for link \"$path\"\n";
print "YES, Link is present" if(-l $path);
}
| [reply] [d/l] |
|
|
|
|
Strange part is that, manually i am able to cd into all modules and respective confm folder to see the properties of the post_REFERENCED_to_EXPERIMENTAL file!!!
| [reply] |
Re: chdir in perl
by newbieperl (Initiate) on Mar 13, 2017 at 16:59 UTC
|
HI
I am new to perl..I am trying to run my script on a windows box. I need to change the script directory to my current path. please help | [reply] |
|
|
| [reply] |