Script BASH con argomenti
In questo articolo vediamo un picolo esempio di script BASH in cui intercettiamo eventuali argomenti passati.
In caso mostriamo un help.
Ecco l'esempio:
#!/bin/bash
function help() {
echo "Usage: script.sh [OPTIONS]"
echo "Options:"
echo " --help Display this help message"
echo " --version Show the script version"
}
if [ "$#" -eq 0 ]; then
help
exit 1
fi
while [[ "$1" != "" ]]; do
case $1 in
--help )
show_help
exit
;;
--version )
echo "Versione 1.0.0"
exit
;;
* )
echo "Opzione non valida: $1"
help
exit 1
esac
shift
done
Qui sotto un pò di comandi:
$ ./test.sh
Usage: script.sh [OPTIONS]
Options:
--help Display this help message
--version Show the script version
$ ./test.sh --version
Versione 1.0.0
$ ./test.sh --ciao
Opzione non valida: --ciao
Usage: script.sh [OPTIONS]
Options:
--help Display this help message
--version Show the script version
Enjoy!
linux bash
Commentami!