1

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.

Dave
  • 2,548

1 Answers1

6

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.

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255