[英]bash scripting: how to find the absolute path of “symlink/..”?
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.从任何位置调用
parent/scripts/hello.sh
后,我希望(在脚本中)找到父目录的完整路径。 In this case parent
.在这种情况下,
parent
。
The main issue is that parent/scripts/..
refers to generic
in unix.主要问题是
parent/scripts/..
指的是 unix 中的generic
。 On the other hand, everything involving regexes is not generic and may be error prone.另一方面,涉及正则表达式的所有内容都不是通用的,并且可能容易出错。
Solutions that don't work:不起作用的解决方案:
`dirname $0`/..
realpath `dirname $0`/..
readlink -f `dirname $0`/..
`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:我用了什么:
dir=$( dirname $( cd `dirname $0` >/dev/null; pwd ) )
Dunno if it is perfect but it seems to behave as expected.不知道它是否完美,但它似乎表现得如预期。
Try this:尝试这个:
basename $(dirname $(dirname $0))
or perhaps just或者也许只是
$(dirname $(dirname $0))
It is unclear if you want parent
alone or its full path.目前尚不清楚您是想要单独的
parent
级还是它的完整路径。
pushd $(dirname $0)
FOO=$(pwd)
popd
That gives you the absolute path of the directory that the script is running in.这为您提供了运行脚本的目录的绝对路径。
I would recommend 1) use pwd -P
which will always give you the physical path, and then navigate with relative path to the other palce This is most safe.我建议 1) 使用
pwd -P
它将始终为您提供物理路径,然后使用相对路径导航到另一个地方这是最安全的。
2) use pwd -L
2)使用
pwd -L
I call my scipt with this: ../script1
and it has a relative call to ../relative/script2
我用这个来称呼我的 scipt:
../script1
并且它有一个对../relative/script2
的相对调用
`(cd $0/.. && pwd)`/relative/script2
is in script1在 script1 中
in bash you could do some string manipulation on $PWD
if that variable is available.在 bash 中,如果该变量可用,您可以对
$PWD
进行一些字符串操作。
For pathname of parent directory, use:对于父目录的路径名,使用:
${PWD%/*}
Other examples:其他示例:
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
What about this:那这个呢:
echo `cd .. && pwd`
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.