如何检查-h
属性是否已传递到shell脚本中?我想在用户拨打myscript.sh -h
时显示帮助信息。如何将帮助方法添加到shell脚本中?
回答
这里是对于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))
我试图在一个函数内部,但是当我尝试运行该函数时,我得到这个错误“basename:invalid option - 'b'”。 看起来它试图将“-bash”传递给带有前导破折号的“basename”。 – 2013-08-07 15:40:53
不使用'“$ FUNCNAME”'而不是'“$ 0”'。另外,添加'本地OPTIND OPTARG' – 2013-08-07 16:08:00
谢谢。 'FUNCNAME'起作用。我将所有函数都放在一个文件中,所以这对于将它们扩展为对其他文件有用的东西来说是完美的。 – 2013-08-07 22:36:11
最好使用bash的getopt设施。请看看这个问答&用于更多的帮助:Using getopts in bash shell script to get long and short command line options
的第一个参数一个shell脚本可以作为变量$1
,所以最简单的实现将是
if [ "$1" == "-h" ]; then
echo "Usage: `basename $0` [somestuff]"
exit 0
fi
但说了什么anubhava。
如果你只有一个选项,以检查它永远是第一个选项($1
),那么最简单的方法是if
与测试([
) 。例如:
if [ "$1" == "-h" ] ; then
echo "Usage: `basename $0` [-h]"
exit 0
fi
注意,对于POSIX兼容=
将工作以及==
。
原因需要$1
被引号括起来的是,如果没有$1
那么shell将尝试运行if [ == "-h" ]
和失败,因为==
只被给予当它被期待两个单独的参数:
$ [ == "-h" ]
bash: [: ==: unary operator expected
作为suggestedbyothers,如果你有多个简单的选项,或者需要你的选项来接受参数,那么你一定要去使用getopts
的额外复杂性。作为快速参考,我喜欢The 60 second getopts tutorial。 †
您可能还想考虑getopt
程序,而不是内置的shell getopts
。它允许在非选项参数(例如foo a b c -v
而不仅仅是foo -v a b c
)之后使用长选项和选项。 This Stackoverflow answer说明如何使用GNU getopt
。
† jeffbyrnes提到original link已经死亡,但幸好0123Å已将其归档。
谢谢!现在我一直很开心地使用getopts,但我也会看看getopt。 – tttppp 2013-03-01 11:57:21
不幸的是,连接到60秒的getopts教程是死的;看来bashcurescancer.com不在了。以下是[Wayback Machine版本]的链接(https://web.archive.org/web/20120409221849/http://bashcurescancer.com/the-60-second-getopts-tutorial.html)。 – jeffbyrnes 2014-03-12 19:33:52
我认为你可以使用情况下这...
case $1 in
-h) echo $usage ;;
h) echo $usage ;;
help) echo $usage ;;
esac
这里是我用它来启动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
;;
这是一个有点怪异,我放在一个部分在一个单独的情况下启动停止重启,但它应该工作
如果你给一个空的选项-d;会不会无限循环? – zerobane 2016-01-17 09:03:11
我只需上传在线手册文件,并用curl
检索它。例如,下面的代码是从Github获取的。
#######################################
# Show help
#
# Globals:
# None
# Arguments:
# None
# Returns:
# None
#
#######################################
show_help() {
curl https://raw.githubusercontent.com/KENJU/shellscript_todo/master/MANUAL | less
}
- 1. 需要帮助将API PUT方法添加到Python脚本
- 2. shell脚本帮助
- 3. Shell脚本帮助
- 4. awk的shell脚本帮助
- 5. shell脚本中的别名帮助
- 6. 如何将多个ips添加到iptables shell脚本中?
- 7. shell脚本削减和sed帮助
- 8. 需要帮助TAR shell脚本
- 9. 关于shell脚本的帮助
- 10. Unix C Shell脚本printf帮助
- 11. 帮助MySQL(.sql)和Shell脚本
- 12. 如何将shell脚本添加到所有jenkins作业?
- 13. 如何将%d添加到argparse帮助文本
- 14. 如何在C#DLL中的方法中添加帮助文档?
- 15. 基本shell脚本帮助。/bin/sh的:来源:找不到
- 16. 如何添加帮助的使用在Python脚本的getopt
- 17. 如何将ActiveRecord属性帮助器方法添加到虚拟属性?
- 18. 将shell脚本中的命令添加到历史记录中?
- 19. Shell脚本帮助,如何跳过错误报告文件?
- 20. Shell脚本添加base_url到php文件
- 21. 如何将引号添加到shell脚本中的字符串中
- 22. 如何将CSS添加到PHP脚本?
- 23. 将localhost的IP和MAC地址添加到shell脚本中
- 24. 编写一个shell脚本以将作业添加到cron中
- 25. 将列添加到shell脚本中的字段
- 26. 将帮助添加到JFrame桨
- 27. 将“帮助”消息添加到字段
- 28. 帮助将属性添加到LinqToSql类
- 29. 需要shell脚本语法帮助“for循环和awk”
- 30. 如何加入shell脚本
相关例子:https://stackoverflow.com/questions/14008125/shell-script-common-template – 2017-10-22 09:27:36