Unix Programming - pwd gives two different results

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > January 2006 > pwd gives two different results





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author pwd gives two different results
nin234@yahoo.com

2006-01-31, 7:21 pm

In my home directory there is a directory
projects_stamp
and I have created a softlink projects in my home directory
projects->projects_stamp

When I do a cd to projects_stamp and do pwd the output is
/export/home/nthomas/projects_stamp

But when I cd to projects from my home directory and do a pwd the
output is

/export/home/nthomas/projects

I am interested in learning why the pwd results are different from an
implementation point of view (ie how cd and pwd are implemented)

Thomas Maier-Komor

2006-01-31, 7:21 pm

nin234@yahoo.com wrote:
> In my home directory there is a directory
> projects_stamp
> and I have created a softlink projects in my home directory
> projects->projects_stamp
>
> When I do a cd to projects_stamp and do pwd the output is
> /export/home/nthomas/projects_stamp
>
> But when I cd to projects from my home directory and do a pwd the
> output is
>
> /export/home/nthomas/projects
>
> I am interested in learning why the pwd results are different from an
> implementation point of view (ie how cd and pwd are implemented)
>


I guess you are using ksh. ksh has a builtin called pwd. Try executing
/usr/bin/pwd instead. Then you will get consistent results.

HTH,
Tom
Stephane CHAZELAS

2006-01-31, 7:21 pm

2006-01-31, 08:19(-08), nin234@yahoo.com:
> In my home directory there is a directory
> projects_stamp
> and I have created a softlink projects in my home directory
> projects->projects_stamp
>
> When I do a cd to projects_stamp and do pwd the output is
> /export/home/nthomas/projects_stamp
>
> But when I cd to projects from my home directory and do a pwd the
> output is
>
> /export/home/nthomas/projects
>
> I am interested in learning why the pwd results are different from an
> implementation point of view (ie how cd and pwd are implemented)

[...]

because "pwd" is actually "pwd -L" (logical). The shell records
the current directory by itself in the $PWD variable (as opposed
as the system's current wd), and uses that to print the current
directory, without actually requesting the current working
directory from the system.

"cd", by default is also "cd -L", so that the shell fakes the
".." (cd .., doesn't go into the directory named "..", it goes
one level up in $PWD).

If you want a real (physical) directory moving around, you need
to pass the -P flag to cd and pwd. In scripts, typically, you
should use cd -P, if you want to behave as a program written in
any other language would.

chdir(dir);

in shell, becomes:

cd -P -- "$dir"

(note that it was introduced by ksh and is now in every POSIX
conformant shell. The feature/problem was not in the Bourne
shell)

--
Stéphane
Nils O. Selåsdal

2006-01-31, 7:21 pm

nin234@yahoo.com wrote:
> In my home directory there is a directory
> projects_stamp
> and I have created a softlink projects in my home directory
> projects->projects_stamp
>
> When I do a cd to projects_stamp and do pwd the output is
> /export/home/nthomas/projects_stamp
>
> But when I cd to projects from my home directory and do a pwd the
> output is
>
> /export/home/nthomas/projects
>
> I am interested in learning why the pwd results are different from an
> implementation point of view (ie how cd and pwd are implemented)
>


http://cm.bell-labs.com/sys/doc/lexnames.html
Describes the problem , among many other things.
Stephane CHAZELAS

2006-01-31, 7:21 pm

2006-01-31, 17:26(+01), Thomas Maier-Komor:
[...]
>
> I guess you are using ksh. ksh has a builtin called pwd. Try executing
> /usr/bin/pwd instead. Then you will get consistent results.

[...]

Not only ksh, also bash, zsh, and ash and all its derivatives.

ksh (and all POSIX shells) have the "-P" option to "pwd" that
should return the same thing as /usr/bin/pwd.

--
Stéphane
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com