Metodo n. 1: utilizzo di bash senza getopt [s]
Due modi comuni per passare argomenti coppia chiave-valore sono:
Bash Space-Separated (ad es. --option argument) (Senza getopt [s])
uso demo-space-separated.sh -e conf -s /etc -l /usr/lib /etc/hosts
cat >/tmp/demo-space-separated.sh <<'EOF'
#!/bin/bash
POSITIONAL=()
while [[ $# -gt 0 ]]
do
key="$1"
case $key in
-e|--extension)
EXTENSION="$2"
shift # past argument
shift # past value
;;
-s|--searchpath)
SEARCHPATH="$2"
shift # past argument
shift # past value
;;
-l|--lib)
LIBPATH="$2"
shift # past argument
shift # past value
;;
--default)
DEFAULT=YES
shift # past argument
;;
*) # unknown option
POSITIONAL+=("$1") # save it in an array for later
shift # past argument
;;
esac
done
set -- "${POSITIONAL[@]}" # restore positional parameters
echo "FILE EXTENSION = ${EXTENSION}"
echo "SEARCH PATH = ${SEARCHPATH}"
echo "LIBRARY PATH = ${LIBPATH}"
echo "DEFAULT = ${DEFAULT}"
echo "Number files in SEARCH PATH with EXTENSION:" $(ls -1 "${SEARCHPATH}"/*."${EXTENSION}" | wc -l)
if [[ -n $1 ]]; then
echo "Last line of file specified as non-opt/last argument:"
tail -1 "$1"
fi
EOF
chmod +x /tmp/demo-space-separated.sh
/tmp/demo-space-separated.sh -e conf -s /etc -l /usr/lib /etc/hosts
output da copia-incolla del blocco sopra:
FILE EXTENSION = conf
SEARCH PATH = /etc
LIBRARY PATH = /usr/lib
DEFAULT =
Number files in SEARCH PATH with EXTENSION: 14
Last line of file specified as non-opt/last argument:
#93.184.216.34 example.com
Bash Equals-Separated (ad es. --option=argument) (Senza getopt [s])
uso demo-equals-separated.sh -e=conf -s=/etc -l=/usr/lib /etc/hosts
cat >/tmp/demo-equals-separated.sh <<'EOF'
#!/bin/bash
for i in "$@"
do
case $i in
-e=*|--extension=*)
EXTENSION="${i#*=}"
shift # past argument=value
;;
-s=*|--searchpath=*)
SEARCHPATH="${i#*=}"
shift # past argument=value
;;
-l=*|--lib=*)
LIBPATH="${i#*=}"
shift # past argument=value
;;
--default)
DEFAULT=YES
shift # past argument with no value
;;
*)
# unknown option
;;
esac
done
echo "FILE EXTENSION = ${EXTENSION}"
echo "SEARCH PATH = ${SEARCHPATH}"
echo "LIBRARY PATH = ${LIBPATH}"
echo "DEFAULT = ${DEFAULT}"
echo "Number files in SEARCH PATH with EXTENSION:" $(ls -1 "${SEARCHPATH}"/*."${EXTENSION}" | wc -l)
if [[ -n $1 ]]; then
echo "Last line of file specified as non-opt/last argument:"
tail -1 $1
fi
EOF
chmod +x /tmp/demo-equals-separated.sh
/tmp/demo-equals-separated.sh -e=conf -s=/etc -l=/usr/lib /etc/hosts
output da copia-incolla del blocco sopra:
FILE EXTENSION = conf
SEARCH PATH = /etc
LIBRARY PATH = /usr/lib
DEFAULT =
Number files in SEARCH PATH with EXTENSION: 14
Last line of file specified as non-opt/last argument:
#93.184.216.34 example.com
Per comprendere meglio la ${i#*=}ricerca di "Rimozione sottostringa" in questa guida . Funzionalmente è equivalente a `sed 's/[^=]*=//' <<< "$i"`quale chiama un sottoprocesso inutile o `echo "$i" | sed 's/[^=]*=//'`che chiama due sottoprocessi inutili.
Metodo n. 2: utilizzo di bash con getopt [s]
da: http://mywiki.wooledge.org/BashFAQ/035#getopts
Limitazioni di getopt (1) ( getoptversioni precedenti, relativamente recenti ):
- non è in grado di gestire argomenti che sono stringhe vuote
- impossibile gestire argomenti con spazi bianchi incorporati
Le getoptversioni più recenti non hanno queste limitazioni.
Inoltre, l'offerta shell POSIX (e altri) getoptsche non ha queste limitazioni. Ho incluso un getoptsesempio semplicistico .
uso demo-getopts.sh -vf /etc/hosts foo bar
cat >/tmp/demo-getopts.sh <<'EOF'
#!/bin/sh
# A POSIX variable
OPTIND=1 # Reset in case getopts has been used previously in the shell.
# Initialize our own variables:
output_file=""
verbose=0
while getopts "h?vf:" opt; do
case "$opt" in
h|\?)
show_help
exit 0
;;
v) verbose=1
;;
f) output_file=$OPTARG
;;
esac
done
shift $((OPTIND-1))
[ "${1:-}" = "--" ] && shift
echo "verbose=$verbose, output_file='$output_file', Leftovers: $@"
EOF
chmod +x /tmp/demo-getopts.sh
/tmp/demo-getopts.sh -vf /etc/hosts foo bar
output da copia-incolla del blocco sopra:
verbose=1, output_file='/etc/hosts', Leftovers: foo bar
I vantaggi di getoptssono:
- È più portatile e funzionerà in altre shell come
dash.
- Può gestire più opzioni singole come
-vf filenamenel tipico modo Unix, automaticamente.
Lo svantaggio di getoptsè che può gestire solo opzioni brevi ( -h, non --help) senza codice aggiuntivo.
C'è un tutorial getopts che spiega cosa significano tutte la sintassi e le variabili. In bash, c'è anche help getopts, che potrebbe essere informativo.
zparseopts -D -E -M -- d=debug -debug=de hanno entrambi-de--debugnella$debugserieecho $+debug[1]restituirà 0 o 1 se vengono utilizzati uno di quelli. Rif: zsh.org/mla/users/2011/msg00350.html