如何在 bash 程序中正确地使路径处理变得健壮?

发布于 2024-08-06 21:28:41 字数 1128 浏览 3 评论 0原文

  • 该脚本有一些硬编码的相对路径。我希望它们相对于脚本位置。

  • 该脚本需要更改当前目录,因为某些其他程序(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

最初的梦 2024-08-13 21:28:41

1)

test $1 == "Debug"

2)
放在

SCRIPT_DIR="$(dirname $0)"
ORIGINAL_DIR="$(pwd)"

脚本的顶部(#! 行之后的第一个非注释行)

使变量相对于脚本绝对:

[ "${VAR/#\//}" != "$VAR" ] || VAR="$SCRIPT_DIR/$VAR"

使其相对于起始目录:

[ "${VAR/#\//}" != "$VAR" ] || VAR="$ORIGINAL_DIR/$VAR"

基本上我们替换前导斜杠与空的 "${VAR/#\//}" 并与 "$VAR" 进行比较,如果不同则 $VAR 为绝对。否则,我们会在前面添加一个要使其成为绝对目录的目录。

1)

test $1 == "Debug"

2)
Put

SCRIPT_DIR="$(dirname $0)"
ORIGINAL_DIR="$(pwd)"

at the top of the script (first non-comment line after #! line)

To make a variable absolute relative to the script:

[ "${VAR/#\//}" != "$VAR" ] || VAR="$SCRIPT_DIR/$VAR"

To make it relative to the starting directory:

[ "${VAR/#\//}" != "$VAR" ] || VAR="$ORIGINAL_DIR/$VAR"

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.

左耳近心 2024-08-13 21:28:41

除了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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文