'simplify bash parameter verification with regexp

I am writing a bash script and I am validating the input parameters that the user provides this way:

if [[ ($# -lt 1 || $# -gt 5)
    || ($# -eq 1 && ! "$1" =~ ^(help|clean)$)
    || ($# -gt 1 &&
        (! "$1" =~ ^(build|clean|install|deploy|remove)$
            || ! "$2" =~ ^(|build|clean|install|deploy|remove)$
            || ! "$3" =~ ^(|build|clean|install|deploy|remove)$
            || ! "$4" =~ ^(|build|clean|install|deploy|remove)$
            || ! "$5" =~ ^(|build|clean|install|deploy|remove)$)) ]]; then
      show_help
      exit 1
fi

The validation rule is the following:

  • running without parameters is not allowed
  • maximum 5 parameters are allowed
  • if only one param is provided then it must be help or clean.
  • if more than one parameters are provided then they must be one of the following: build, clean, install, deploy, remove

Is there any way to simplify the last 5 conditions and avoid the repetitions?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source