Linux bash 脚本:如何找到“符号链接/..”的绝对路径?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6267753/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
bash scripting: how to find the absolute path of "symlink/.."?
提问by dagnelies
Given two files:
给定两个文件:
generic/scripts/hello.sh
parent/scripts -> generic/scripts
Upon calling parent/scripts/hello.sh
from any location, I would like (in the script) to find the full path of the parent directory. In this case parent
.
parent/scripts/hello.sh
从任何位置调用时,我希望(在脚本中)找到父目录的完整路径。在这种情况下parent
。
The main issue is that parent/scripts/..
refers to generic
in unix. On the other hand, everything involving regexes is not generic and may be error prone.
主要问题是parent/scripts/..
指generic
在unix中。另一方面,涉及正则表达式的所有内容都不是通用的,并且可能容易出错。
Solutions that don't work:
不起作用的解决方案:
`dirname dir=$( dirname $( cd `dirname basename $(dirname $(dirname $(dirname $(dirname echo `cd .. && pwd`
))
))
` >/dev/null; pwd ) )
`/..
realpath `dirname pushd $(dirname `(cd ${PWD%/*}
/.. && pwd)`/relative/script2
)
FOO=$(pwd)
popd
`/..
readlink -f `dirname me@host:/tmp/bash_string/ma ni-pu_la/tion$ echo -e \
"$PWD or ${PWD} : " $PWD \
"\n${PWD##/*} : " ${PWD##/*} \
"\n${PWD##*/} : " ${PWD##*/} \
"\n${PWD#/*} : " ${PWD#/*} \
"\n${PWD#*/} : " ${PWD#*/} \
"\n${PWD%%/*} : " ${PWD%%/*} \
"\n${PWD%%*/} : " ${PWD%%*/} \
"\n${PWD%/*} : " ${PWD%/*} \
"\n${PWD%*/} : " ${PWD%*/} \
"\n" # Gives :
$PWD or ${PWD} : /tmp/bash_string/ma ni-pu_la/tion
${PWD##/*} :
${PWD##*/} : tion
${PWD#/*} : tmp/bash_string/ma ni-pu_la/tion
${PWD#*/} : tmp/bash_string/ma ni-pu_la/tion
${PWD%%/*} :
${PWD%%*/} : /tmp/bash_string/ma ni-pu_la/tion
${PWD%/*} : /tmp/bash_string/ma ni-pu_la
${PWD%*/} : /tmp/bash_string/ma ni-pu_la/tion
`/..
`cd *something*/..; pwd`
`perl ... abs_path(...)`
All these will point to generic
and not parent
because of the symbolic link.
所有这些都将指向generic
而不是parent
因为符号链接。
Everything involving regular expressions are not adaptable/generic, may fail for more complexes paths. There might be other ..
and symlinks in the path, you want the grand-parent, it's a directory name involving ..
, you call it via $PATH...
涉及正则表达式的所有内容都不是适应性的/通用的,对于更复杂的路径可能会失败。..
路径中可能还有其他符号链接,您想要祖父,它是一个涉及 的目录名..
,您可以通过 $PATH 调用它...
Moreover, I would like it to work in any case, even when it is called via $PATH
.
此外,我希望它在任何情况下都能工作,即使它是通过$PATH
.
Any simple safe solution for this simple problem? I mean it's just getting the parent directory after all!
这个简单的问题有什么简单的安全解决方案吗?我的意思是它毕竟只是获取父目录!
What I used:
我用过的:
##代码##Dunno if it is perfect but it seems to behave as expected.
不知道它是否完美,但它似乎表现得如预期。
采纳答案by jlliagre
Try this:
尝试这个:
##代码##or perhaps just
或者也许只是
##代码##It is unclear if you want parent
alone or its full path.
目前尚不清楚您是想要parent
单独还是完整路径。
回答by patapizza
What about this:
那这个呢:
##代码##回答by Op De Cirkel
I would recommend
1) use pwd -P
which will always give you the physicalpath, and then navigate with relative path to the other palceThis is most safe.
我建议 1) 使用pwd -P
它总是会给你物理路径,然后用相对路径导航到另一个地方这是最安全的。
2) use pwd -L
2) 使用 pwd -L
回答by PaulMurrayCbr
That gives you the absolute path of the directory that the script is running in.
这为您提供了运行脚本的目录的绝对路径。
回答by User
I call my scipt with this: ../script1
and it has a relative call to ../relative/script2
我用这个调用我的 scipt:../script1
它有一个相对的调用../relative/script2
is in script1
在脚本 1 中
回答by imme
in bash you could do some string manipulationon $PWD
if that variable is available.
在 bash 中,如果该变量可用,您可以进行一些字符串操作$PWD
。
For pathname of parent directory, use:
对于父目录的路径名,请使用:
##代码##Other examples :
其他例子:
##代码##