如果命令在 Unix shell 中失败,如何中止?

How to abort if command fails in Unix shell?

我不太擅长 shell 脚本编写,但仍然找不到解决方案。我想 运行 一个命令 gulp tslint 如果它 returns 错误,则中止脚本。

到目前为止,我尝试了 gulp tslint || exit 1 但没有成功。我认为它以某种方式返回 true,即使它在命令提示符下 运行 它有错误,例如

[12:30:14] Starting 'tslint'...
[12:30:14] Finished 'tslint' after 9.49 ms
[12:30:16] [gulp-tslint] error (quotemark) payment-types.ts[18, 53]: " should be '

我怎样才能让它发挥作用?

感谢任何帮助。谢谢!

这看起来像 gulp 问题,而不是 shell 问题。由于 gulp 成功完成(即到达出口而没有引发错误),它不会告诉 shell 任何错误。解决此问题的最佳方法是重新配置 gulp 以将 lint 失败解释为错误。

你能post你的gulpfile.js吗?特别是你的 tslint 任务。

大概是这样

gulp.task('tslint', function() {
    gulp.src("**/*.ts")
        .pipe(tslint.report('prose'))
});

您可以通过将其更改为:

来指示您的记者在出错时失败
gulp.task('tslint', function() {
    gulp.src("**/*.ts")
        .pipe(tslint.report('prose', {emitError: true}))
});

编辑:查看 gulp-tslint README 以获取有关 emitError

的更多详细信息