0

I'm using bash. How can I exit with a non-success code if my script has the wrong number of arguments?

I have this

#!/bin/bash

if [ "$#" -ne 3 ]; then
    echo "Should be three parameters to this script -- [CWD driver-directory side-file]"
fi

but then the script continues.

1
3

You just need to call exit:

#!/bin/bash

if [ "$#" -ne 3 ]; then
    echo "Should be three parameters to this script -- [CWD driver-directory side-file]"
    exit 9
fi

Using any non-negative integer between 1 and 255 that you like or want.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.