如何在 bash 程序中正确地使路径处理变得健壮?
该脚本有一些硬编码的相对路径。我希望它们相对于脚本位置。
该脚本需要更改当前目录,因为某些其他程序(cmake)需要它。
该脚本采用一些(可能相对于调用者)路径作为参数,并将它们传递给该程序,它们应该被去相关化。
问题是内联的:
#!/bin/sh
# First arg should be Release or Debug
# TODO test for that.
if test -n "$1"; then # BTW how to test whether $1 is Debug or Release?
BUILD_TYPE="$1"
else
BUILD_TYPE="Release"
fi
# Set install prefix to current directory, unless second argument is given.
if test -n "$2"; then
INSTALL_PREFIX="$2" # How to derelativize this path argument?
else
INSTALL_PREFIX=bin # How to make this path relative to script location?
fi
# Make build directory and do cmake, make, make install.
mkdir -p build/${BUILD_TYPE} && # How to make this path relative to script location?
cd build/${BUILD_TYPE} &&
cmake -D CMAKE_BUILD_TYPE=${BUILD_TYPE} \
-D CMAKE_INSTALL_PREFIX=${INSTALL_PREFIX} \ # Possible relative to caller current directory.
../../ && # Relative to scrip position.
make -j4 &&
make install
这是一个常见问题还是我以非标准方式做某事?
The script have some hard-coded relative paths. I would like them to be relative to script position.
The script needs to change current directory as some other program (cmake) needs it.
The script takes some (possibly relative to caller) paths as an argument and it passes them to that program, they should be derelativized.
Questions are inline:
#!/bin/sh
# First arg should be Release or Debug
# TODO test for that.
if test -n "$1"; then # BTW how to test whether $1 is Debug or Release?
BUILD_TYPE="$1"
else
BUILD_TYPE="Release"
fi
# Set install prefix to current directory, unless second argument is given.
if test -n "$2"; then
INSTALL_PREFIX="$2" # How to derelativize this path argument?
else
INSTALL_PREFIX=bin # How to make this path relative to script location?
fi
# Make build directory and do cmake, make, make install.
mkdir -p build/${BUILD_TYPE} && # How to make this path relative to script location?
cd build/${BUILD_TYPE} &&
cmake -D CMAKE_BUILD_TYPE=${BUILD_TYPE} \
-D CMAKE_INSTALL_PREFIX=${INSTALL_PREFIX} \ # Possible relative to caller current directory.
../../ && # Relative to scrip position.
make -j4 &&
make install
Is it a common problem or am I doing something in a non-standard way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
1)
2)
放在
脚本的顶部(
#!
行之后的第一个非注释行)使变量相对于脚本绝对:
使其相对于起始目录:
基本上我们替换前导斜杠与空的
"${VAR/#\//}"
并与"$VAR"
进行比较,如果不同则$VAR
为绝对。否则,我们会在前面添加一个要使其成为绝对目录的目录。1)
2)
Put
at the top of the script (first non-comment line after
#!
line)To make a variable absolute relative to the script:
To make it relative to the starting directory:
Basically we replace a leading slash with empty
"${VAR/#\//}"
and compare with"$VAR"
, if they are different then$VAR
is absolute. Otherwise we prepend a directory that we want to make it absolute.除了Douglas Leeder 说,我建议始终将变量用双引号引起来,以防止带有空格字符的路径弄乱您的脚本。
In addition to what Douglas Leeder said, I’d recommend to always surround your variables in double quotes to prevent paths with space characters messing up your script.