我正在制作一个预提交脚本。它看起来是这样的:
function presubmit() {
gradle test android
gradle test ios
gradle test server
git push origin master
}如果任何测试失败,我希望函数退出,这样它就不会将bug推到git上。多么?
发布于 2018-08-19 06:30:02
我这样做的方法是在函数中的每个命令后添加&& \ (除了最后一个命令)。
function presubmit() {
gradle test android && \
gradle test ios && \
gradle test server && \
git push origin master
}发布于 2016-11-05 03:19:32
您可以这样做:
# declare a wrapper function for gradle
gradle() {
command gradle "$@" || exit 1
}
presubmit() {
gradle test android
gradle test ios
gradle test server
git push origin master
}
declare -xf presubmit gradle调用子subshell中的函数,如下所示:
( presubmit )发布于 2017-10-06 10:44:18
我会让脚本更细粒度:
#!/bin/bash
function test() {
gradle test android
gradle test ios
gradle test server
}
function push() {
git push origin master
}
# this subshell runs similar to try/catch
(
# this flag will make to exit from current subshell on any error inside test or push
set -e
test
push
)
# you catch errors with this if
if [ $? -ne 0 ]; then
echo "We have error"
exit $?
fi我们只在测试和推送中跟踪错误。您可以在测试和推送运行的subshell之外添加更多操作。您还可以通过这种方式为错误添加不同的作用域(让我们将其视为try/catch)
https://stackoverflow.com/questions/40429865
复制相似问题