Re^2: Getting the absolute path of a script, without PWD
by LanX (Saint) on Jul 15, 2021 at 15:40 UTC
|
| [reply] |
|
|
% cd /tmp
% mkdir foo
% cd foo
% mkdir dir1
% ln -s dir1 sym1
% cd sym1
% echo PWD
/tmp/foo/sym1
% pwd
/tmp/foo/sym1
% perl -MCwd=getcwd -E'say getcwd'
/tmp/foo/dir1
% perl -MCwd=cwd -E'say cwd'
/tmp/foo/dir1
But pwd and PWD sometime give different results too.
UPDATE:The difference is pwd can be instructed to return "logical" and "physical" version, and the logical version still gets information from PWD. Cwd's documentation: "The cwd() is the most natural form for the current architecture. For most systems it is identical to `pwd` (but without the trailing line terminator)." might perhaps be improved by saying: "The cwd() is the most natural form for the current architecture. For most systems it is identical to `pwd -P` (but without the trailing line terminator). | [reply] |
|
|
This seems to be a portability issue.
cwd()."\n" eq `pwd -L`
- holds for termux, win10
- fails for my (old) ubuntu in a VM
I'd say file a bugreport against Cwd and in the meantime resort to `pwd` or `pwd -L` on systems where it fails.
update
after further investigation, it seems that `pwd` inside Perl doesn't default like pwd in bash ... which is unexpected
lanx@ubuntu14-large:/tmp/sym$ perl -MCwd -E'say `pwd` '
/tmp/dir
lanx@ubuntu14-large:/tmp/sym$ pwd
/tmp/sym
lanx@ubuntu14-large:/tmp/sym$ perl -MCwd -E'say `pwd -L` '
/tmp/sym
lanx@ubuntu14-large:/tmp/sym$
so strictly speaking, this is not a bug in Cwd, since cwd() returns the same like `pwd` (which does not always -L )
The question is why is bash's pwd defaulting differently inside Perl?
| [reply] [d/l] [select] |
|
|
|
|
|
|
|
|
|
|
Re^2: Getting the absolute path of a script, without PWD
by perlancar (Hermit) on Jul 15, 2021 at 12:02 UTC
|
One reason is for logging path and the symlinked version is more meaningful and/or hides unwanted detail. For example I have a device /media/USER/1234-5678 mounted but I also keep /media/USER/MORE-MEANINGFUL-NAME symlinked to it and want to log the filenames inside the symlinked version. Okay not a great example as I should've just relabeled the removable volume in the first place, but that's the only example I can think of right now. There are other cases I've encountered previously. | [reply] |
|
|
| [reply] |
|
|
Yes, that's just a curiosity. I'm using PWD right now.
| [reply] |
|
|
| [reply] |
|
|
This will result in a permutation of possible paths. How would you know which path is the correct one?
| [reply] |