Exemple complet
#!/bin/bash #set -x QUIET=1 CURRENT_PRICE="" POSITIONAL_ARGS=() function usage() { echo "Usage: $0 [--quiet] [--aws-profile <my_aws_profile>] [--aws-bucket <my-aws-bucket>] [--airport <airport_id>] [--departure-date <date_slash>] [-departure-time <time_colon>] [--return-date <date_slash>] [--return-time <time_colon>] [--cars <cars_comma>] [--ntfy-token <ntfy_token>]" } while [[ $# -gt 0 ]]; do case $1 in -profile|--aws-profile) AWS_PROFILE="$2" shift # past argument shift # past value ;; -bucket|--aws-bucket) AWS_BUCKET="$2" shift # past argument shift # past value ;; -q|--quiet) QUIET=0 shift # past argument ;; -ap|--airport) TRIP_AIRPORT="$2" shift # past argument shift # past value ;; -dd|--departure-date) TRIP_DEPARTURE_DATE="$2" shift # past argument shift # past value ;; -dt|--departure-time) TRIP_DEPARTURE_TIME="$2" shift # past argument shift # past value ;; -rd|--return-date) TRIP_RETURN_DATE="$2" shift # past argument shift # past value ;; -rt|--return-time) TRIP_RETURN_TIME="$2" shift # past argument shift # past value ;; -ca|--cars) IFS=',' read -r -a CARS <<< "$2" shift # past argument shift # past value ;; -cp|--current-price) CURRENT_PRICE="$2" shift # past argument shift # past value ;; -ntfy|--ntfy-token) NTFY_TOKEN="$2" shift # past argument shift # past value ;; -*|--*) echo "Unknown option $1" exit 1 ;; *) POSITIONAL_ARGS+=("$1") # save positional arg shift # past argument ;; esac done set -- "${POSITIONAL_ARGS[@]}" # restore positional parameters MISSING_ARGS_FLAG=1 MISSING_ARGS_TEXT="Error: Missing parameters:\n" if [ -z "${AWS_PROFILE}" ]; then MISSING_ARGS_TEXT="${MISSING_ARGS_TEXT} - AWS Profile\n" MISSING_ARGS_FLAG=0 fi if [ -z "${AWS_BUCKET}" ]; then MISSING_ARGS_TEXT="${MISSING_ARGS_TEXT} - AWS Bucket\n" MISSING_ARGS_FLAG=0 fi if [ -z "${TRIP_AIRPORT}" ]; then MISSING_ARGS_TEXT="${MISSING_ARGS_TEXT} - Airport code\n" MISSING_ARGS_FLAG=0 fi if [ -z "${TRIP_DEPARTURE_DATE}" ]; then MISSING_ARGS_TEXT="${MISSING_ARGS_TEXT} - Departure date\n" MISSING_ARGS_FLAG=0 fi if [ -z "${TRIP_DEPARTURE_TIME}" ]; then MISSING_ARGS_TEXT="${MISSING_ARGS_TEXT} - Departure time\n" MISSING_ARGS_FLAG=0 fi if [ -z "${TRIP_RETURN_DATE}" ]; then MISSING_ARGS_TEXT="${MISSING_ARGS_TEXT} - Return date\n" MISSING_ARGS_FLAG=0 fi if [ -z "${TRIP_RETURN_TIME}" ]; then MISSING_ARGS_TEXT="${MISSING_ARGS_TEXT} - Return time\n" MISSING_ARGS_FLAG=0 fi if [ -z "${CARS}" ]; then MISSING_ARGS_TEXT="${MISSING_ARGS_TEXT} - Cars\n" MISSING_ARGS_FLAG=0 fi if [ ${MISSING_ARGS_FLAG} -eq 0 ]; then echo -e "${MISSING_ARGS_TEXT}" usage exit 1 fi
Le script de référence est présent sur le repository suivant :