Skip to content
Advertisement

How to abort if command fails in Unix shell?

I’m not very great with shell scripting and still couldn’t find a solution to this. I want to run a command gulp tslint and if it returns errors, abort the script.

So far I tried gulp tslint || exit 1 with no success. I think it somehow is returning true, even though it has errors when i run it on command prompt e.g.

[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 '

How can i make it work?

Any help appreciated. Thanks!

Advertisement

Answer

This looks like a gulp question, rather than a shell question. As gulp completes successfully (i.e. reaches an exit without raising an error), it doesn’t tell the shell that anything is wrong. The best way to address this is to reconfigure gulp to interpret lint failures as errors.

Could you post your gulpfile.js? Specifically your tslint task.

It probably looks like

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

You can instruct your reporter to fail on error by changing this to:

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

edit: look at the gulp-tslint README for more details on emitError

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement