in reply to chdir not working

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.

Replies are listed 'Best First'.
Re^2: chdir not working
by ikegami (Patriarch) on Oct 05, 2007 at 19:51 UTC

    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