Linux 如何在shell脚本中扩展相对路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11621639/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 13:53:27  来源:igfitidea点击:

How to expand relative paths in shell script

linuxbash

提问by Jimm

I am writing a script to set environment variables on linux 2.6 using bash. So the script contains commands like:

我正在编写一个脚本来使用 bash 在 linux 2.6 上设置环境变量。所以脚本包含如下命令:

export SRC_DIR=..
export LIBPATH=${SRC_DIR}/lib

the problem is that when i try to do echo $LIBPATH, it shows "../lib" as opposed to expanding the SRC_DIR to full path. I would really like the script to print something like /home/x/lib as opposed to ../lib.

问题是,当我尝试执行 echo $LIBPATH 时,它显示“../lib”,而不是将 SRC_DIR 扩展到完整路径。我真的很希望脚本打印类似 /home/x/lib 而不是 ../lib 的东西。

UPDATE The script should evaluate SRC_DIR to be one directory upwards from the script's location and not the current directory from where the script is invoked

更新脚本应该将 SRC_DIR 评估为从脚本位置向上的一个目录,而不是调用脚本的当前目录

回答by jman

Do this instead:

改为这样做:

export SRC_DIR=`pwd`;

Update:

更新:

Since you want path relative to the script's location on the filesystem, use this instead:

由于您想要相对于脚本在文件系统上的位置的路径,请改用:

export SRC_DIR=`dirname 
export SRC_DIR=$(cd ..; pwd)
`

Update2:

更新2:

Your script must be invoked directly and not as bash /path/to/script.shor source foo.sh. Add a shebang line, add execute permissions and invoke the script directly.

您的脚本必须直接调用,而不是作为bash /path/to/script.shsource foo.sh。添加shebang行,添加执行权限并直接调用脚本。

回答by Todd A. Jacobs

Change Directory in Subshell

在子shell中更改目录

There's a little trick you can use to get the absolute path from a relative path without changing the present working directory. The trick is to move to the relative path in a subshell, and then expand the working directory. For example:

您可以使用一个小技巧从相对路径获取绝对路径,而无需更改当前工作目录。诀窍是移动到子shell 中的相对路径,然后展开工作目录。例如:

# Using /usr/bin/dirname.
export SRC_DIR=$(cd "$(dirname "
SCRIPT_DIR=$(readlink -f ${0%/*})
")/.."; pwd) # Using the "remove matching suffix pattern" parameter expansion. export SRC_DIR=$(cd "${0%/*}/.."; pwd)

Relative Paths from Script Instead of Invocation Directory

来自脚本而不是调用目录的相对路径

To change to a relative path from a script's location, rather than the current working directory, you can use a parameter expansionor the dirnameutility. I prefer dirname, since it's a little more explicit. Here are both examples.

要从脚本位置更改为相对路径,而不是当前工作目录,您可以使用参数扩展dirname实用程序。我更喜欢dirname,因为它更明确一点。这里有两个例子。

export SRC_DIR=`echo $HOME/bin/`

回答by choroba

I usually use

我通常使用

# The path you want to get information on... (for readability)
your_path=..

# Using /bin/readlink (resolves symlinks)
export SRC_DIR=$(readlink --canonicalize $your_path)

# Using /usr/bin/dirname (keeps symlinks)
export SRC_DIR=$(cd $your_path ; pwd)

It should return the full path to the script, and even resolves all the links along the way.

它应该返回脚本的完整路径,甚至解析沿途的所有链接。

回答by Bernard Edlington

When I do this I use echo like so:

当我这样做时,我像这样使用 echo:

function getPwd() {
    $(cd ; [[ $? -ne 0 ]] && exit 1 || echo echo $PWD;)
    return $?
}

回答by mbells

The readlinkcommand is capable of not only resolving symlinks, but also canonicalizing relative paths. Be careful, since you may not want the behaviour of resolving symlinks in all your scripts. If you don't want to resolve symlinks, pwdwould be the best; note the use of a subshell, so the cd command does not affect the working directory in the main shell.

readlink命令不仅能够解析符号链接,还能够规范化相对路径。小心,因为您可能不希望在所有脚本中解析符号链接的行为。如果您不想解决符号链接,pwd那就最好了;注意子shell的使用,所以cd命令不会影响主shell中的工作目录。

##代码##

回答by Zeph

For keeping error code:

为了保持错误代码:

##代码##

回答by Lerring

Digging up this old thread to add what i've found to work nicely:

挖掘这个旧线程以添加我发现可以很好地工作的内容:

export SRC_DIR = `realpath ..`

export SRC_DIR = `realpath ..`

see more information here:

在此处查看更多信息:

http://man7.org/linux/man-pages/man3/realpath.3.html

http://man7.org/linux/man-pages/man3/realpath.3.html