Gestire le opzioni di uno script con getopts

From RVM Wiki
Jump to navigation Jump to search

Questo script consente le opzioni -d -t -h e -v.

Se si inserisce un'opzione sbagliata, viene stampato un messaggio con l'help.

#!/bin/bash

USAGE="backup-dvd\t[-d] [-t] [-h]\n
\t\t-d = Debug mode\n
\t\t-t = Test mode (backup /usr/lib for testing)\n
\t\t-h = Show this help\n
"

while getopts ":dthv" options; do
        case $options in
                "d" )   #debug
                        set -x
                        DEBUG="YES"
                        VERBOSE="-v"
                        TO_NULL="NO"
                        echo "RUNNING IN DEBUG MODE"
                        ;;

                "t" )   #test
                        TESTMODE="YES"
                        START_PATH="/usr/lib"
                        echo "RUNNING IN TEST MODE"
                        ;;

                "h" )   # help
                        echo -e $USAGE
                        exit 127
                        ;;

                "v" )   #show version and exits
                        echo -e "backup-dvd $VERSION\n"
                        exit 127
                        ;;

                * )     #options not recognised
                        echo -e $USAGE
                        exit 127
                        ;;
        esac
done

Riferimenti