Twitter iPhone pliant OnePlus 11 PS5 Disney+ Orange Livebox Windows 11

cp, chemins relatifs et liens symboliques

14 réponses
Avatar
mpg
Bonjour,

Soient deux répertoires A et B (dans mon home, mettons), C un
sous-répertoire de A, et D un lien symbolique placé dans B et pointant vers
C. Si, depuis B, je fais 'cd D', et que j'affiche PWD,
j'obtiens /home/moi/B/D. Si je fais 'cd ..' je reviens dans B. Si par
contre, je choisis un fichier F de C, et que, au moment où mon PWD
est /home/moi/B/D, je fais 'cp F ..', F se retrouve dans A.

Je trouve ça assez troublant : est-ce que c'est un comportement normal ?
Quelle est la logique derrière ça ?

Je connais l'option -P de cd qui permet de savoir toujours où on est pour de
vrai et évite les ambiguïtés, mais parfois j'utilise des liens symboliques
pointant vers un répertoire « profond » dans la hiérarchie, et c'est
justement pour ne pas avoir à me farcir le chemin complet à chaque fois (en
même temps, il y a une notion de répertoire nommé sous zsh je crois, ça
doit aider...)

Manuel.

4 réponses

1 2
Avatar
Stephane Chazelas
On Wed, 05 Dec 2007 00:55:26 +0100, Cyrille Lefevre wrote:
[...]
PWD
Set by the shell to be an absolute pathname of the
current working directory, containing no components
of type symbolic link, no components that are dot,
and no components that are dot-dot when the shell is
initialized. If an application sets or unsets the
value of PWD , the behaviors of the cd and pwd
utilities are unspecified.

http://www.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html#tag_02_05_03


oui, mais la page suivante ne fait pas référence aux liens symboliques :

http://www.opengroup.org/onlinepubs/009695399/utilities/sh.html#tag_04_128_08
[...]


Je faisais reference au fait que ksh/bash heritent PWD de
l'environnement allant a l'encontre de POSIX.

Je ne conteste pas le fait qu'ils assignent un chemin logique a
$PWD ou que cd .. ne fait pas un chdir("..").

--
Stephane


Avatar
Cyrille Lefevre
On Wed, 05 Dec 2007 00:55:26 +0100, Cyrille Lefevre wrote:
[...]
PWD
Set by the shell to be an absolute pathname of the
current working directory, containing no components
of type symbolic link, no components that are dot,
and no components that are dot-dot when the shell is
initialized. If an application sets or unsets the
value of PWD , the behaviors of the cd and pwd
utilities are unspecified.

http://www.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html#tag_02_05_03
oui, mais la page suivante ne fait pas référence aux liens symboliques :


http://www.opengroup.org/onlinepubs/009695399/utilities/sh.html#tag_04_128_08
[...]


Je faisais reference au fait que ksh/bash heritent PWD de
l'environnement allant a l'encontre de POSIX.


je ne vois rien dans la norme qui interdise cela.

pour aller plus loin, la variable $PWD est supposée symboliser le
répertoire courant lorsqu'elle existe, à cet effet, en hériter ne
me semble pas incohérent.

pour autant, cela peut effectivement poser un pb si un prog fork un
shell alors qu'il a fait un chdir() sans avoir mis a jour $PWD.

toutefois, je viens de faire un test sous cygwin sans rencontrer le pb
que met en avant ! à tester sous de vrais unix... la réponse demain :)

$ cat c.c
int main() {
putenv("PWD=/");printf("%sn",getenv("PWD"));return(system(getenv("SHELL")));
}
$ pwd
/home/Cyrille

# bash 3.2.25.17
$ SHELL=/bin/bash ./c
/
bash-3.2$ pwd
/home/Cyrille
bash-3.2$ /bin/pwd

# pdksh 5.2.14.2
$ SHELL=/bin/ksh ./c
/
$ pwd
/home/Cyrille

# ksh88f
$ SHELL=/usr/local/bin/ksh88 ./c
/
$ pwd
/home/Cyrille

# ksh93s
$ SHELL=/opt/ast/bin/ksh ./c
/
$ pwd
/home/Cyrille

# zsh 4.3.4
$ SHELL=/bin/zsh ./c
/
$P$Gpwd
/home/Cyrille

idem avec cc.c :
int main() {
putenv("PWD=/");printf("%sn",getenv("PWD"));return(execlp(getenv("SHELL"),0));
}

comme ça, on ne peut pas accuser system() de faire quoique ce soit.

Regards, Cordialement,

Cyrille Lefevre.
--
mailto:Cyrille.Lefevre-news%
supprimer "%nospam% et ".invalid" pour me repondre.
remove "%nospam" and ".invalid" to answer me.



Avatar
Stephane Chazelas
On Thu, 06 Dec 2007 03:13:52 +0100, Cyrille Lefevre wrote:
On Wed, 05 Dec 2007 00:55:26 +0100, Cyrille Lefevre wrote:
[...]
PWD
Set by the shell to be an absolute pathname of the
current working directory, containing no components
of type symbolic link, no components that are dot,
and no components that are dot-dot when the shell is
initialized. If an application sets or unsets the
value of PWD , the behaviors of the cd and pwd
utilities are unspecified.

http://www.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html#tag_02_05_03
oui, mais la page suivante ne fait pas référence aux liens symboliques :


http://www.opengroup.org/onlinepubs/009695399/utilities/sh.html#tag_04_128_08
[...]


Je faisais reference au fait que ksh/bash heritent PWD de
l'environnement allant a l'encontre de POSIX.


je ne vois rien dans la norme qui interdise cela.


Ben si puisque le shell est censé initialiser $PWD avec un
chemin qui ne contient ni ".." ni symlink.

$ cd /tmp
$ ln -s . here
$ env PWD=/tmp/here/here/here bash -c 'echo $PWD'
/tmp/here/here/here

(plein de symlinks).

pour aller plus loin, la variable $PWD est supposée symboliser le
répertoire courant lorsqu'elle existe, à cet effet, en hériter ne
me semble pas incohérent.


A ceci pres que ca a des repercusions genantes comme dans

env PWD=/tmp/here/here bash -c 'cd .. && pwd'

Cela dit, meme avec cette non-conformance fixee, il faut
quand-meme utiliser cd -P

comme dans

bash -c 'cd "$1"' - /tmp/here/../etc


pour autant, cela peut effectivement poser un pb si un prog fork un
shell alors qu'il a fait un chdir() sans avoir mis a jour $PWD.

toutefois, je viens de faire un test sous cygwin sans rencontrer le pb
que met en avant ! à tester sous de vrais unix... la réponse demain :)

$ cat c.c
int main() {
putenv("PWD=/");printf("%sn",getenv("PWD"));return(system(getenv("SHELL")));
}
$ pwd
/home/Cyrille
[...]


Ben, ya pas de symlink la-dedans, d'ailleurs, y-a-t'il des
symlinks sous cygwin?

--
Stephane




Avatar
Jean-Louis Hamel


Ben, ya pas de symlink la-dedans, d'ailleurs, y-a-t'il des
symlinks sous cygwin?



Sous cygwin on peut créer des symlinks (avec "ln -s ...") et, ce qui est
intéressant, ces symlinks sont aussi des raccourcis Windows (alors que
les raccourcis Windows créés avec Windows ne sont pas des symlinks pour
cygwin).
"cd -P" fonctionne correctement sous cygwin avec ces symlinks.

--
JLH

1 2