了解调用命令的Bash if语句

时间:2017-05-17 19:28:46

标签: bash if-statement

有谁知道这是做什么的:

if ! /fgallery/fgallery -v -j3 /images /usr/share/nginx/html/ "${GALLERY_TITLE:-Gallery}"; then
  mkdir -p /usr/share/nginx/html

我理解第一部分是说如果/fgallery/fgallery目录不存在,但在此之后它对我来说并不清楚。

1 个答案:

答案 0 :(得分:3)

在Bash中,我们可以通过这种方式基于命令的退出状态构建if

if command; then
  echo "Command succeeded"
else
  echo "Command failed"
fi

then部分在命令以0退出时执行,否则else部分执行。

您的代码正是这样做的。

可以改写为:

/fgallery/fgallery -v -j3 /images /usr/share/nginx/html/ "${GALLERY_TITLE:-Gallery}"; fgallery_status=$?
if [ "$fgallery_status" -ne 0 ]; then
  mkdir -p /usr/share/nginx/html
fi

但前一种结构更优雅,更不易出错。

请参阅以下帖子:

相关问题