shell脚本参数解析 [英] Shell script argument parsing
问题描述
有一些关于这种事情的问题,但让我们来想象我们的目标是一个通用的Linux系统既getopt的和getopts的安装(我不是说我们既可以使用,但他们似乎流行)
There are a number of questions about this sort of thing but lets imagine we are targeting a generic Linux system with both getopt and getopts installed (not that we'll use either, but they seem popular)
我如何分析这两个长(--example | --example简单的选项)和短argruments(-e | -esimple,例如| -e简单的例子)
How do I parse both long (--example | --example simple-option) and short argruments (-e | -esimple-example | -e simple-example)
推荐答案
您想要使用 getopt的
长和短选项。从工作code的例子:
You want to use getopt
with long and short options. An example from working code:
# Parse arguments
TEMP=$(getopt -n $PROGRAM_NAME -o p:P:cCkhnvVS \
--long domain-password:,pop3-password:\
,create,cron,kill,help,no-sync-passwords,version,verbose,skip-pop3 \
-- "$@")
# Die if they fat finger arguments, this program will be run as root
[ $? = 0 ] || die "Error parsing arguments. Try $PROGRAM_NAME --help"
eval set -- "$TEMP"
while true; do
case $1 in
-c|--create)
MODE="CREATE"; shift; continue
;;
-C|--cron)
MODE="CRON"; shift; continue
;;
-k|--kill)
MODE="KILL"; shift; continue
;;
-h|--help)
usage
exit 0
;;
-n|--no-sync-passwords)
SYNC_VHOST=0; shift; continue
;;
-p|--domain-password)
DOMAIN_PASS="$2"; shift; shift; continue
;;
-P|--pop3-password)
POP3_PASS="$2"; shift; shift; continue
;;
-v|--version)
printf "%s, version %s\n" "$PROGRAM_NAME" "$PROGRAM_VERSION"
exit 0
;;
-v|--verbose)
VERBOSE=1; shift; continue
;;
-S|--skip-pop3)
SKIP_POP=1; shift; continue
;;
--)
# no more arguments to parse
break
;;
*)
printf "Unknown option %s\n" "$1"
exit 1
;;
esac
done
请注意,死亡
是pviously(未显示)。
Note, die
is a function that was defined previously (not shown).
的 -n
选项告诉getopt说明报告错误,我的程序的名称,而不是 getopt的
。 -o
定义的短选项(:
后一个选项,表示需要参数)的列表和 - 长
指定的长选项(对应为了短期期权)
The -n
option tells getopt to report errors as the name of my program, not as getopt
. -o
defines a list of short options (:
after an option indicates a needed argument) and --long
specifies the list of long options (corresponding in order to the short options).
剩下的只是一个简单的开关,称移
适当提前参数指针。请注意,调用转变;移位;
仅仅是难改的习惯。在目前的现代世界,变速2
可能就够了。
The rest is just a simple switch, calling shift
appropriately to advance the argument pointer. Note, calling shift; shift;
is just a die hard habit. In the currently modern world, shift 2
would probably suffice.
现代getopt的是覆盖新平台pretty一致的,但是你可能会遇到在较旧的(大约pre红帽9)系统中的一些可移植性问题。参见男人的getopt
有关向后兼容的信息。但是它不太可能你会碰到需要它。
The modern getopt is pretty consistent over newer platforms, however you may encounter some portability problems on older (circa pre Redhat 9) systems. See man getopt
for information about backwards compatibility. However it's unlikely that you'll run into the need for it.
最后,解析选项后,可以再次致电:
Finally, after parsing options, you can once again call:
eval set -- "$@"
这将参数指针移动到别的留在命令行上的getopt做解析选项之后。然后,您可以只移
来继续阅读它们。例如,如果一个命令是这样的:
This will move the argument pointer to anything else left on the command line after getopt was done parsing options. You can then just shift
to keep reading them. For instance, if a command looked like this:
./foo --option bar file1.txt file2.txt file3.txt
不要忘了做一个方便的 -h / --help
选项打印新奇特的选择,一旦你就大功告成了。 :)如果你作出这样的输出 help2man 友好,你有一个瞬间手册页去与你的新工具。
Don't forget to make a handy -h / --help
option to print your new fancy options once you're done. :) If you make that output help2man friendly, you have an instant man page to go with your new tool.
修改
在大多数发行版,你可以找到更多的例子 getopt的
code在的/ usr /共享/ DOC / UTIL Linux的/例子
,它应该已经默认安装。
On most distributions, you can find more example getopt
code in /usr/share/doc/util-linux/examples
, which should have been installed by default.
这篇关于shell脚本参数解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!