쉘 스크립트에 도움말 메소드를 추가하려면 어떻게해야합니까?
-h
속성이 쉘 스크립트로 전달 되었는지 어떻게 확인 합니까? 사용자가 전화를 걸 때 도움말 메시지를 표시하고 싶습니다 myscript.sh -h
.
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))
함수 안에서 이것을 사용하려면 :
"$FUNCNAME"
대신에 사용$(basename "$0")
local OPTIND OPTARG
전화하기 전에 추가getopts
쉘 스크립트에 대한 첫 번째 인수는 변수로 사용할 수 $1
있으므로 가장 간단한 구현은 다음과 같습니다.
if [ "$1" == "-h" ]; then
echo "Usage: `basename $0` [somestuff]"
exit 0
fi
그러나 아누 바바가 말한 것.
여기 내가 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
;;
esac
별도의 경우에 시작 중지 다시 시작을 배치 한 것이 조금 이상하지만 작동해야합니다.
검사 할 단일 옵션 만 있고 항상 첫 번째 옵션 ( $1
) 인 경우 가장 간단한 옵션은 if
테스트 ( [
)입니다. 예를 들면 다음과 같습니다.
if [ "$1" == "-h" ] ; then
echo "Usage: `basename $0` [-h]"
exit 0
fi
Note that for posix compatibility =
will work as well as ==
.
The reason the $1
needs to be enclosed in quotes is that if there is no $1
then the shell will try to run if [ == "-h" ]
and fail because ==
has only been given a single argument when it was expecting two:
$ [ == "-h" ]
bash: [: ==: unary operator expected
As suggested by others, if you have more than a single simple option, or need your options to accept arguments, then you should definitely go for the extra complexity of using getopts
. As a quick reference, I like The 60 second getopts tutorial.†
You may also want to consider the getopt
program instead of the built in shell getopts
. It allows the use of long options and options after non option arguments (e.g. foo a b c -v
rather than just foo -v a b c
). This Stackoverflow answer explains how to use GNU getopt
.
† jeffbyrnes mentioned that the original link died but thankfully the way back machine had archived it.
Better to use getopt facility of bash. Please look at this Q&A for more help: Using getopts in bash shell script to get long and short command line options
i think you can use case for this...
case $1 in
-h) echo $usage ;;
h) echo $usage ;;
help) echo $usage ;;
esac
I simply upload the Online Manual file and retrieve it with curl
. For example, below code is retrieving from Github.
#######################################
# Show help
#
# Globals:
# None
# Arguments:
# None
# Returns:
# None
#
#######################################
show_help () {
curl https://raw.githubusercontent.com/KENJU/shellscript_todo/master/MANUAL | less
}
참고URL : https://stackoverflow.com/questions/5474732/how-can-i-add-a-help-method-to-a-shell-script
'IT박스' 카테고리의 다른 글
Angular.js 속성 지시문에 여러 속성을 어떻게 전달합니까? (0) | 2020.07.23 |
---|---|
C #에서 상수 사전 만들기 (0) | 2020.07.23 |
C 데이터 유형은“대부분의 컴퓨터에서 직접 지원”되는 방식은 무엇입니까? (0) | 2020.07.23 |
단위 테스트, NUnit 또는 Visual Studio? (0) | 2020.07.23 |
'git gui'를 실행할 때“Loose Object”팝업을 건너 뛰는 방법 (0) | 2020.07.23 |