Jak dodać metodę pomocy do skryptu powłoki?
Jak sprawdzić, czy atrybut -h
został przekazany do skryptu powłoki? Chcę wyświetlić komunikat pomocy, gdy użytkownik wywoła myscript.sh -h
.
7 answers
Oto przykład bash:
usage="$(basename "$0") [-h] [-s n] -- program to calculate the answer to life, the universe and everything
where:
-h show this help text
-s set the seed value (default: 42)"
seed=42
while getopts ':hs:' option; do
case "$option" in
h) echo "$usage"
exit
;;
s) seed=$OPTARG
;;
:) printf "missing argument for -%s\n" "$OPTARG" >&2
echo "$usage" >&2
exit 1
;;
\?) printf "illegal option: -%s\n" "$OPTARG" >&2
echo "$usage" >&2
exit 1
;;
esac
done
shift $((OPTIND - 1))
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-05-17 12:43:07
Pierwszy argument skryptu powłoki jest dostępny jako zmienna $1
, więc najprostszą implementacją będzie
if [ "$1" == "-h" ]; then
echo "Usage: `basename $0` [somestuff]"
exit 0
fi
Ale to, co powiedział anubhava.Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-03-07 20:11:09
Oto część, której używam do uruchomienia serwera vnc
#!/bin/bash
start() {
echo "Starting vnc server with $resolution on Display $display"
#your execute command here mine is below
#vncserver :$display -geometry $resolution
}
stop() {
echo "Killing vncserver on display $display"
#vncserver -kill :$display
}
#########################
# The command line help #
#########################
display_help() {
echo "Usage: $0 [option...] {start|stop|restart}" >&2
echo
echo " -r, --resolution run with the given resolution WxH"
echo " -d, --display Set on which display to host on "
echo
# echo some stuff here for the -a or --add-options
exit 1
}
################################
# Check if parameters options #
# are given on the commandline #
################################
while :
do
case "$1" in
-r | --resolution)
if [ $# -ne 0 ]; then
resolution="$2" # You may want to check validity of $2
fi
shift 2
;;
-h | --help)
display_help # Call your function
exit 0
;;
-d | --display)
display="$2"
shift 2
;;
-a | --add-options)
# do something here call function
# and write it in your help function display_help()
shift 2
;;
--) # End of all options
shift
break
;;
-*)
echo "Error: Unknown option: $1" >&2
## or call function display_help
exit 1
;;
*) # No more options
break
;;
esac
done
######################
# Check if parameter #
# is set too execute #
######################
case "$1" in
start)
start # calling function start()
;;
stop)
stop # calling function stop()
;;
restart)
stop # calling function stop()
start # calling function start()
;;
*)
# echo "Usage: $0 {start|stop|restart}" >&2
display_help
exit 1
;;
To trochę dziwne, że umieściłem start stop restart w osobnym przypadku, ale powinno działać
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-06-14 14:09:38
Jeśli masz tylko jedną opcję do sprawdzenia i zawsze będzie to pierwsza opcja ($1
), najprostszą opcją jest if
z testem ([
). Na przykład:
if [ "$1" == "-h" ] ; then
echo "Usage: `basename $0` [-h]"
exit 0
fi
Zauważ, że dla zgodności z posix =
będzie działać tak samo dobrze jak ==
.
Powód, dla którego $1
musi być zamknięty w cudzysłowach jest taki, że jeśli nie ma $1
wtedy powłoka spróbuje uruchomić if [ == "-h" ]
i zawiedzie, Ponieważ ==
podano tylko jeden argument, gdy oczekiwano dwóch:
$ [ == "-h" ]
bash: [: ==: unary operator expected
Jako zasugerował przez inne , jeśli masz więcej niż jedną prostą opcję lub potrzebujesz opcji do zaakceptowania argumentów, zdecydowanie powinieneś wybrać dodatkową złożoność użycia getopts
. Jako szybkie odniesienie, Lubię 60 second getopts tutorial.†
Możesz również rozważyć getopt
program zamiast wbudowanej powłoki getopts
. Pozwala na użycie długich opcji i opcji PO non argumenty opcji (np. foo a b c -v
a nie tylko foo -v a b c
). ta odpowiedź Stackoverflow wyjaśnia, jak używać GNU getopt
.
† jeffbyrnes wspomniał, że oryginalny linkumarł, ale na szczęście maszyna way backzarchiwizowała go.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-23 11:54:41
Lepiej użyć funkcji getopt bash. Proszę spojrzeć na to pytanie i więcej pomocy: Używanie getopts w skrypcie powłoki bash, aby uzyskać długie i krótkie opcje wiersza poleceń
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-23 12:02:53
Myślę, że możesz użyć case ' a...
case $1 in
-h) echo $usage ;;
h) echo $usage ;;
help) echo $usage ;;
esac
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-08-07 22:47:51
Po prostu wgrywam plik instrukcji Online i pobieram go za pomocą curl
.
Na przykład, poniższy kod jest pobierany z Github.
#######################################
# Show help
#
# Globals:
# None
# Arguments:
# None
# Returns:
# None
#
#######################################
show_help () {
curl https://raw.githubusercontent.com/KENJU/shellscript_todo/master/MANUAL | less
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-10-19 11:12:53