bash脚本中的mvn if语句 [英] mvn in bash script if statement

查看:69
本文介绍了bash脚本中的mvn if语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在bash脚本中运行命令mvn clean.但我想将其放在if语句中.如果清理无法正常运行,我希望使用echo语句退出bash脚本. 这是导致问题的代码: 如果[mvn clean];然后

I want to run the command mvn clean in a bash script. But I want to put it in an if statement. If the clean does not run properly I would like to exit out of the bash script with an echo statement. Here is the code that is causing the problem: if [ mvn clean ]; then

我尝试将$(mvn clean)放在if语句中,但是终端说参数太多.有人知道这是否可能吗?谢谢!

I tried putting $(mvn clean) inside the if statement but there were too many arguments says the terminal. Does anyone know if this is possible? Thanks!

推荐答案

这就是您想要的:

mvn clean
if [ "$?" -ne 0 ]; then
    echo "Maven Clean Unsuccessful!"
    exit 1
fi

说明:

  • $? is a special shell variable that contains the exit code (whether it terminated successfully, or not) of the most immediate recently executed command.
  • -ne is an option to the test builtin [. It stands for "not equal". So here we are testing if the exit code from mvn clean is not equal to zero.
  • echo "Maven Clean Unsucccessful!" - If this is the case, then we output some indicative message, and exit the script itself with an errant exit code.

当您执行$(mvn clean)时,它会生成一个新的子Shell来运行mvn clean,然后只需将该子Shell中输出到stdout的所有内容从运行mvn clean转储到在其中使用$(...)父外壳.

When you do $(mvn clean), that instead spawns a new subshell to run mvn clean, then simply dumps everything that was output to stdout in that subshell from running mvn clean to where $(...) was used in the parent shell.

或者,您可以执行以下操作:

Alternatively, you can do:

mvn clean || { echo "Maven Clean Unsuccessful"; exit 1; }

这只是做同一件事的简写语法糖.

Which is just shorthand syntactic sugar for doing the same thing.

这篇关于bash脚本中的mvn if语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆