参见英文答案 >
Using getopts in bash shell script to get long and short command line options28个答案我想支持短和长选项在bash脚本,所以一个可以:
$ foo -ax --long-key val -b -y SOME FILE NAMES
可能吗?
getopt支持长选项。
原文链接:https://www.f2er.com/bash/389067.htmlhttp://linux.about.com/library/cmd/blcmdl1_getopt.htm
这里是一个使用你的参数的例子:
#!/bin/bash OPTS=`getopt -o axby -l long-key: -- "$@"` if [ $? != 0 ] then exit 1 fi eval set -- "$OPTS" while true ; do case "$1" in -a) echo "Got a"; shift;; -b) echo "Got b"; shift;; -x) echo "Got x"; shift;; -y) echo "Got y"; shift;; --long-key) echo "Got long-key,arg: $2"; shift 2;; --) shift; break;; esac done echo "Args:" for arg do echo $arg done
输出$ foo -ax –long-key val -b -y某些文件的名称:
Got a Got x Got long-key,arg: val Got b Got y Args: SOME FILE NAMES