[英]Bash: handling mass arguments
I'd like to be able to handle multiple arguments to a given flag no matter what the order of flags is. 无论标记的顺序如何,我都希望能够处理给定标记的多个参数。 Do you guys think this is acceptable?
你们认为这可以接受吗? Any improvements?
有什么改善吗?
So: 所以:
$ ./script -c opt1 opt2 opt3 -b foo
opt1 opt2 opt3
foo
Code: 码:
echo_args () {
echo "$@"
}
while (( $# > 0 )); do
case "$1" in
-b)
echo $2
;;
-c|--create)
c_args=()
# start looping from this flag
for arg in ${@:2}; do
[ "${arg:0:1}" == "-" ] && break
c_args+=("$arg")
done
echo_args "${c_args[@]}"
;;
*)
echo "huh?"
;;
esac
shift 1
done
The getopts utility shall retrieve options and option-arguments from a list of parameters. getopts实用程序应从参数列表中检索选项和选项参数。
$ cat script.sh
cflag=
bflag=
while getopts c:b: name
do
case $name in
b) bflag=1
bval="$OPTARG";;
c) cflag=1
cval="$OPTARG";;
?) printf "Usage: %s: [-c value] [-b value] args\n" $0
exit 2;;
esac
done
if [ ! -z "$bflag" ]; then
printf 'Option -b "%s" specified\n' "$bval"
fi
if [ ! -z "$cflag" ]; then
printf 'Option -c "%s" specified\n' "$cval"
fi
shift $(($OPTIND - 1))
printf "Remaining arguments are: %s\n" "$*"
Note the Guideline 8: When multiple option-arguments are specified to follow a single option, they should be presented as a single argument, using commas within that argument or <blank>s within that argument to separate them. 请注意准则8: 当指定多个选项参数以跟随单个选项时,应将它们显示为单个参数,并使用该参数内的逗号或该参数内的<blank>分隔它们。
$ ./script.sh -c "opt1 opt2 opt3" -b foo
Option -b "foo" specified
Option -c "opt1 opt2 opt3" specified
Remaining arguments are:
The standard links are listed below: 下面列出了标准链接:
I noticed in the comments that you don't want to use any of these. 我在评论中注意到您不想使用其中任何一个。 What you could do is set all of the arguments as a string, then sort them using a loop, pulling out the ones you want to set as switched and sorting them using if statements.
您可以做的是将所有参数设置为字符串,然后使用循环对它们进行排序,取出要设置为switch的参数,并使用if语句对它们进行排序。 It is a little brutish, but it can be done.
这有点野蛮,但是可以做到。
#!/bin/bash
#set all of the arguments as a variable
ARGUMENTS=$@
# Look at each argument and determine what to do with it.
for i in $ARGUMENTS; do
# If the previous loop was -b then grab the value of this argument
if [[ "$bgrab" == "1" ]]; then
#adds the value of -b to the b string
bval="$bval $i"
bgrab="0"
else
# If this argument is -b, prepare to grab the next argument and assign it
if [[ "$i" == "-b" ]]; then
bgrab="1"
else
#Collect the remaining arguments into one list per your example
RemainingArgs="$RemainingArgs $i"
fi
fi
done
echo "Arguments: $RemainingArgs"
echo "B Value: $bval"
I use something similar in a lot of my scripts because there are a significant amount of arguments that can be fed into some of them, and the script needs to look at each one to figure out what to do. 我在很多脚本中都使用了类似的东西,因为其中可以输入大量的参数,并且脚本需要查看每个参数以弄清楚该怎么做。 They can be out of order or not exist at all and the code still has to work.
它们可能乱序或根本不存在,并且代码仍必须正常工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.