经过几天的研究,我仍然无法找出解析 .sh 脚本中的 cmdline args 的最佳方法。根据我的参考资料, getopts cmd 是可行的方法,因为它“提取并检查开关而不干扰位置参数变量。意外的开关或缺少参数的开关会被识别并报告为错误。”
位置参数(例如 2 - $@、$# 等)显然在涉及空格时效果不佳,但可以识别常规参数和长参数(-p 和 --longparam)。我注意到,当传递带有嵌套引号的参数时,这两种方法都会失败(“这是““引号””的示例。”)。这三个代码示例中哪一个最能说明处理 cmdline args 的方法?专家不推荐 getopt 函数,所以我试图避免它!
示例1:
#!/bin/bash
for i in "$@"
do
case $i in
-p=*|--prefix=*)
PREFIX=`echo $i | sed 's/[-a-zA-Z0-9]*=//'`
;;
-s=*|--searchpath=*)
SEARCHPATH=`echo $i | sed 's/[-a-zA-Z0-9]*=//'`
;;
-l=*|--lib=*)
DIR=`echo $i | sed 's/[-a-zA-Z0-9]*=//'`
;;
--default)
DEFAULT=YES
;;
*)
# unknown option
;;
esac
done
exit 0
示例2:
#!/bin/bash
echo ‘number of arguments’
echo "\$#: $#"
echo ”
echo ‘using $num’
echo "\$0: $0"
if [ $# -ge 1 ];then echo "\$1: $1"; fi
if [ $# -ge 2 ];then echo "\$2: $2"; fi
if [ $# -ge 3 ];then echo "\$3: $3"; fi
if [ $# -ge 4 ];then echo "\$4: $4"; fi
if [ $# -ge 5 ];then echo "\$5: $5"; fi
echo ”
echo ‘using $@’
let i=1
for x in $@; do
echo "$i: $x"
let i=$i+1
done
echo ”
echo ‘using $*’
let i=1
for x in $*; do
echo "$i: $x"
let i=$i+1
done
echo ”
let i=1
echo ‘using shift’
while [ $# -gt 0 ]
do
echo "$i: $1"
let i=$i+1
shift
done
[/bash]
output:
bash> commandLineArguments.bash
number of arguments
$#: 0
using $num
$0: ./commandLineArguments.bash
using $@
using $*
using shift
#bash> commandLineArguments.bash "abc def" g h i j*
示例3:
#!/bin/bash
while getopts ":a:" opt; do
case $opt in
a)
echo "-a was triggered, Parameter: $OPTARG" >&2
;;
\?)
echo "Invalid option: -$OPTARG" >&2
exit 1
;;
:)
echo "Option -$OPTARG requires an argument." >&2
exit 1
;;
esac
done
exit 0
我发现
getopt
的使用是最简单的。它提供了对参数的正确处理,否则会很棘手。例如,getopt
将知道如何处理在命令行上指定为 --arg=option
或 --arg option
的长选项的参数。
在解析传递给 shell 脚本的任何输入时有用的是使用
"$@"
变量。请参阅 bash 手册页了解这与 $@
有何不同。它确保您可以处理包含空格的参数。
这是我如何编写脚本来解析一些简单的命令行参数的示例:
#!/bin/bash
args=$(getopt -l "searchpath:" -o "s:h" -- "$@")
eval set -- "$args"
while [ $# -ge 1 ]; do
case "$1" in
--)
# No more options left.
shift
break
;;
-s|--searchpath)
searchpath="$2"
shift
;;
-h)
echo "Display some help"
exit 0
;;
esac
shift
done
echo "searchpath: $searchpath"
echo "remaining args: $*"
并像这样使用来显示空格和引号被保留:
user@machine:~/bin$ ./getopt_test --searchpath "File with spaces and \"quotes\"."
searchpath: File with spaces and "quotes".
remaining args: other args
有关使用
getopt
的一些基本信息可以在这里找到
如果您想避免使用
getopt
,您可以使用这个不错的快速方法:
##
注释(根据您的意愿自定义)。log.sh
#!/bin/sh
## $PROG 1.0 - Print logs [2017-10-01]
## Compatible with bash and dash/POSIX
##
## Usage: $PROG [OPTION...] [COMMAND]...
## Options:
## -i, --log-info Set log level to info (default)
## -q, --log-quiet Set log level to quiet
## -l, --log MESSAGE Log a message
## Commands:
## -h, --help Displays this help and exists
## -v, --version Displays output version and exists
## Examples:
## $PROG -i myscrip-simple.sh > myscript-full.sh
## $PROG -r myscrip-full.sh > myscript-simple.sh
PROG=${0##*/}
LOG=info
log_info() {
LOG=info
}
log_quiet() {
LOG=quiet
}
log() {
[ $LOG = info ] && echo "$1"; return 1 ## number of args used
}
help() {
sed -E -e '/^##/!d' -e 's/^## ?//' -e "s/[$]PROG/$PROG/g" "$0"; exit 0
}
version() {
help | head -1
}
[ $# = 0 ] && help
while [ $# -gt 0 ]; do
CMD=$(grep -m 1 -Po "^## *$1, --\K[^= ]*|^##.* --\K${1#--}(?:[= ])" log.sh | sed -e "s/-/_/g")
if [ -z "$CMD" ]; then echo "ERROR: Command '$1' not supported"; exit 1; fi
shift; eval "$CMD" "$@" || shift $? 2> /dev/null
done
运行此命令:
./log.sh --log yep --log-quiet -l nop -i -l yes
产生此输出:
yep
yes
顺便说一下:它与posix兼容!