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

I've encounter a problem using `pwd` in a Perl script that happens to be in a directory managed by the automounter, amd. As I understand backticks in Perl on Linux, the commands are executed in a sh. So, if I..

/bin/sh
cd /net/mymachine/home/myuser
pwd
(output: /net/mymachine/home/myuser)
echo $PWD
(output: /net/mymachine/home/myuser)

..which is what I expect. But, if I..

perl -e 'print `pwd`'

..I get the output '/.automount/mymachine/root/home/myuser', which is the true resolved mountpoint that is managed by amd, and not what I expect. As a workaround, I have written a subroutine the detects and converts the result, but I should not have to do this. I am familiar with amd, perl, and sh but do not understand why this is happening. What's going on?

BTW, the only shell I know of that does this translation is csh.

Replies are listed 'Best First'.
Re: `pwd`, sh, and the amd automounter
by ctilmes (Vicar) on Oct 17, 2003 at 18:25 UTC
    What does this say:
    perl -e 'print "$ENV{PWD}\n"'
    or try using Cwd with some of its various options for getting current working directory.
Re: `pwd`, sh, and the amd automounter (/bin/)
by tye (Sage) on Oct 17, 2003 at 19:41 UTC

    Your shell's 'pwd' command is a built-in that doesn't resort to /bin/pwd unless it has to. Even if you get Perl to call the shell's pwd instead of /bin/pwd, the shell will have started up not knowing where it is and will have to resort to the same steps that /bin/pwd uses and so will report the (correct) directory that you don't want to see.

    You see, the shell keeps track of 'cd' commands (it has to process these itself as a child process calling chdir won't change the current working directory of its parent process) and stores the perceived current working directory which it reports if you ask it for $PWD or run 'pwd'.

    The best advice I've come up with is to have your Perl program check $ENV{PWD} and, if it is the same as "." (stat both of them and compare device number and inode number -- if you are on a Unixish enough file system), then you can use it instead of redetermining the current working directory any of the other ways.

                    - tye
Re: `pwd`, sh, and the amd automounter
by sgifford (Prior) on Oct 17, 2003 at 19:51 UTC

    Some shells try to be clever and remember how you got to the directory you're currently in, and show you that when you type pwd. `pwd will run the /bin/pwd command, which will give you the honest-to-goodness directory you're in. It's resolved by looking at the current directory, then opening the parent directory and scanning it to get the name of the current directory, and so forth until it's in the root directory. Because of how it works, it will always return the One True Pathname, as it has no way of knowing about another path you may have taken to end up in this directory.

    The Cwd module and $ENV{PWD} might help, though:

    #!/usr/bin/perl -w use strict; use Cwd qw(chdir); chdir("/service/dnscache"); print "\$ENV{PWD}=$ENV{PWD}\n"; print "`pwd`=",`pwd`;
    </code>
Re: `pwd`, sh, and the amd automounter
by ptkdb (Monk) on Oct 17, 2003 at 18:36 UTC
    start csh run perl -e 'print `pwd`' do you get the same behavior?