amitrajvarma has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's minimum standard of quality and will not be displayed.

Replies are listed 'Best First'.
Re: chdir not working
by bruceb3 (Pilgrim) on Oct 05, 2007 at 11:52 UTC
    How about you quit with the guessing and just use the return value of the chdir function and the $! variable in string context.
    $ perl -e 'chdir "no existant directory" or die "chdir failed: $!\n"'

    Which outputs this helpful message;

    chdir failed: No such file or directory
Re: chdir not working
by svenXY (Deacon) on Oct 05, 2007 at 11:17 UTC
    Hi,
    could you please add code tags around the actual perl code? thanks.

    Could be numerous reasons...
    hmmm, does /vob/ios/sys/obj-ppc-c3750 exist?
    if ( -d '/vob/ios/sys/obj-ppc-c3750') { chdir("/vob/ios/sys/obj-ppc-c3750"); } else { print "/vob/ios/sys/obj-ppc-c3750 does not exist\n"; }

    You don't need all those chomps either. They are only necessary if the string contains a newline in the end - not the case for $ARGV[0] or for the other assignments.

    you create and print $path, then you chdir to somewhere else. Is that intended? What is $path good for then?

    Regards,
    svenXY
      Hi c3750 directory exist, but script is not doing CHDIR Any way I can do it Thanks Amit
Re: chdir not working
by Crackers2 (Parson) on Oct 05, 2007 at 19:38 UTC

    Also remember that your program can not effect the working directory of its parent. So if you are trying to run this from a shell prompt and are expecting the current directory of your shell to be changed after your script exits, that won't work.

    user@host:/mytempdir> perl -e'use Cwd; chdir("/"); print getcwd() . "\ +n";' / user@host:/mytempdir>

    In the example above you can see that the chdir did work, but after the script exits the cwd is back to what it was.

      One solution is to pass the information to the parent and let it change the path.

      • Unix, single command:

        chdir `perl -e 'print quotemeta("/some/path")'`
      • Unix, multiple commands:

        $ cat script.pl print("set var=val\n"); print("set foo=bar\n"); print("cd /some/path\n"); $ script.pl > temp_file && source temp_file && rm temp_file

        I'm sure it can be done without a temp file, but my sh scripting is rusty.

      • Windows, multiple commands:

        >type script.pl print("set var=val\n"); print("set foo=bar\n"); print("cd \\some\\path\n"); >for /f "usebackq delims=" %%f in (`script.pl`) do %%f