2014-10-04 115 views
1

請參閱下面的Unix命令。 當我們處理軟鏈接時,每個目錄的根目錄可能有多條路徑。 那麼在這種情況下,如何計算pwdcd ..?這意味着目錄路徑不再是無狀態的,對吧?當你有軟鏈接時,「cd ..」和「pwd」是什麼意思?

$ cd ~ 
$ mkdir a b 
$ cd a 
$ ln -s ~/b b 
$ cd b 
$ pwd 
/home/myuser/a/b 

$ cd .. 
$ pwd 
/home/myuser/a 
+0

這種問題將在[unix.se]上討論。 – terdon 2014-10-04 14:29:04

回答

0

該命令總是根據軟鏈接後面的實際(已解析)目錄進行計算。 當你

$ cd b ; you end up in the directory pointed by b 

從這裏的任何命令是基於這個新的位置解決

1

請看下面的例子:

[[email protected] ~]$ pwd 
/home/myuser 
[[email protected] ~]$ mkdir a b 
[[email protected] ~]$ cd a 
[[email protected] a]$ ln -s ~/b b 
[[email protected] a]$ cd b 
[[email protected] b]$ pwd 
/home/myuser/a/b 
[[email protected] b]$ pwd -P 
/home/myuser/b 
[[email protected] b]$ echo $$ 
2432 
[[email protected] b]$ ls -l /proc/2432/cwd 
lrwxrwxrwx 1 myuser myuser 0 Oct 4 04:10 /proc/2432/cwd -> /home/myuser/b 
[[email protected] b]$ 
[[email protected] b]$ 
[[email protected] b]$ pwd 
/home/myuser/a/b 
[[email protected] b]$ cd -P .. 
[[email protected] ~]$ pwd 
/home/myuser 
[[email protected] ~]$ 
[[email protected] ~]$ env | grep "PWD" 
PWD=/home/myuser 
OLDPWD=/home/myuser/a/b 

查看選項-P從bash的手冊光盤:

-P  If set, the shell does not follow symbolic links when executing commands such as cd that 
        change the current working directory. It uses the physical directory structure instead. 
        By default, bash follows the logical chain of directories when performing commands which 
        change the current directory. 

正如你所看到的,目前的dir保持內核是你真正的目錄(/ proc/2432/cwd - >/home/myuser/b),但bash可以做任何想要遵循符號鏈接或不是, ,因爲cd是bash內部命令。