Dernière modification : 01/01/2021

Template basique d'un script Bash

 

Template basique d'un script Bash permettant de parser des paramètres d'entrée.

Paramètres inclus : --help (aide), --version (version), -e ou --example (sauvegarde la saleur associée). Méthodes incluses : (show_error_exit et show_parameter_error_exit) et d'affichages de l'utilisation (show_usage)

Affichage de l'aide du script :

bash monScript.sh --help

Le script bash "monScript.sh" :

#!/bin/bash

########################################################################################################################
############################################### Variable Initialization ################################################
########################################################################################################################
# General variables
APPLICATION_NAME=$(basename $0)
APPLICATION_AUTHOR="XXXXX YYYYYY"
APPLICATION_VERSION="v0.0.1"
APPLICATION_LICENCE="Copyright (C) ${APPLICATION_AUTHOR} 2020. All rights reserved."

# Error codes
DEFAULT_GENERAL_ERROR=2;
DEFAULT_PARAMETER_ERROR=2;

# Example value parameter
EXAMPLE=""
########################################################################################################################
############################################### Function Initialization ################################################
########################################################################################################################
# Show the usage
function show_usage(){
  echo "Usage ${APPLICATION_NAME} [OPTION]";
  echo "Description ...."
  echo "Option :"
  echo " -e,--example [VALUE]   example description";
  # Other parameters
  echo " --version              version";
  echo " --help                 display this help and exit";
  echo "";
  echo "Exit status:";
  echo " 0  if OK,";
  echo " 1  if minor problems (e.g., cannot access subdirectory),"
  echo " 2  if serious trouble (e.g., cannot access command-line argument)."
}

# Show an error with the parameter name and exit the script
# param $1 : the parameter
# param $2 : (optional) the exit error code, default to $DEFAULT_PARAMETER_ERROR
function show_parameter_error_exit(){
  local PARAMETER="${1}";
  local ERROR_CODE=${2:-$DEFAULT_PARAMETER_ERROR}
  echo "${APPLICATION_NAME}: unknown option -- ${PARAMETER}" >&2;
  echo "Try '${APPLICATION_NAME} --help' for more information." >&2;
  exit ${ERROR_CODE};
}

# Show an error with the error description and exit the script
# param $1 : the error description
# param $2 : (optional) the exit error code, default to $DEFAULT_GENERAL_ERROR
function show_error_exit(){
  local DESCRIPTION="${1}";
  local ERROR_CODE=${2:-$DEFAULT_GENERAL_ERROR}
  echo "An error has occurred." >&2;
  echo "Description: ${DESCRIPTION}" >&2;
  exit ${ERROR_CODE};
}

# Show the version
function show_version(){
  echo "${APPLICATION_NAME} ${APPLICATION_VERSION}"
  echo "${APPLICATION_LICENCE}"
}
########################################################################################################################
################################################## Bash process begin ##################################################
########################################################################################################################

##################### Parse parameters begin #####################
set -- "$@" # Secure parameters

while [[ $# -gt 0 ]] # Parse parameter
do
  case ${1} in
  -e|--example) # Option -e or --example
      EXAMPLE=${2} # Save the value
      shift 2;
  ;;
  --help) # Show usage
      show_usage;
      shift;
      exit 0;
  ;;
  --version) # Show version
      show_version;
      shift;
      exit 0;
  ;;
  *) # Unknown parameter
      show_parameter_error_exit ${1};
  ;;
  esac
done
# Test the presence of parameters

# Obligatory example parameter
[ -n "${EXAMPLE}" ] || show_error_exit "The example parameter is not found";
##################### Parse parameters end #####################

########################################################################################################################
################################################### Bash process end ###################################################
########################################################################################################################

exit 0