• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1425
  • Last Modified:

chdir in perl script not working well

the script hold list of directories :

#!/usr/local/bin/perltk -w
 ...


foreach $dir (@all_dirs)  {

$pwd = `pwd`;
printf "curr dir : $pwd\n";
chomp $pwd;

printf "Open dir: $pwd/$dir \n";
chdir("$pwd/$dir");

$pwd2 =  `pwd`;
printf "curr dir now : $pwd2\n";



    $ipg_file = 'dev00_gmii_ipg_data.fws';
    if (-e 'dev00_gmii_ipg_data.fws') {
        $ipg_file = 'dev00_gmii_ipg_data.fws';
    } elsif (-e 'dev00_ipg_data.fws') {
        $ipg_file = 'dev00_ipg_data.fws';
    } else {
        print "Files dev00_gmii_ipg_data.fws and dev00_ipg_data.fws don't exists!!!!\n\n\n";
        exit;
    }
..
}

but as you can see , I added print for locations and lothing changed , beside , it is NOT changing dirctory
0
Talmash
Asked:
Talmash
  • 2
2 Solutions
 
jmcgOwnerCommented:
Please change this line:

chdir("$pwd/$dir");

to

print "chdir to $pwd/$dir\n";
chdir("$pwd/$dir") or warn( "chdir failed: $!");


and show us the output. As it is, we don't have enough information to go on.

Most likely, the solution to the problem is to have the directories in your list @all_dirs be absolute path names rather than relative path nmaes so you don't have to keep track of your initial directory after you start doing chdir.
0
 
mikezoneCommented:
I don't know if the exit() you have in your loop is intentional, and I'm guessing it's supposed to be outside of the loop. Otherwise, the program will probably exit after searching the first path.

A wonderful module that a co-worker introduced to me was Cwd, which has such commands as getcwd() and abs_path(), which gets the current working directory, and converts relative paths to absolute paths, respectively. Best of all, it's portable (unlike `pwd`) and it's part of the standard Perl distribution.

You could:

  use Cwd qw( getcwd abs_path );

  $pwd = getcwd();
  $dir = abs_path( $dir );
  chdir( $dir ) or die "Can't change paths to '$dir'";

to fix the potential relative path problem in a portable fashion.

But it looks like what you really want is a way to find one of two .fws data file, and for that I suggest the File::Find module, also part of the standard Perl distribution. For that, I suggest:

  use Cwd;
  use File::Find;

  $pwd = getcwd();
  my $ipg_file;

  find( \&wanted, $pwd );

  sub wanted {
    if( /^dev00_(gmii_)?ipg_data.fws$/ ) {
      $ipg_file = $_;
    }
  }

There's a lot of magic in File::Find, and it's a great module to learn to use. For more information on either module, check the Camel book, perldoc File::Find, and perldoc Cwd.

hth,

- m.
0
 
jmcgOwnerCommented:
Nothing has happened on this question in over 8 months. It's time for cleanup!

My recommendation, which I will post in the Cleanup topic area, is to
split points between jmcg [25 pts] and mikezone [25 pts].

Please post any comments here within the next seven days. Moderators check comments here before acting on the recommendation. Experts: silence will likely be taken as assent.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

jmcg
EE Cleanup Volunteer
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now