$@ 中 args 的 bash 参数大小写
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14062895/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
bash argument case for args in $@
提问by Gabe Rainbow
I have a script with a long list of OPTIONAL arguments. some have associated values.
我有一个包含一长串可选参数的脚本。有些有关联的值。
Such as:
如:
.script --first 2012-12-25 --last 2012-12-26 --copy --remove
.script --first 2012-12-25
Thus the following case statement:
因此,以下 case 语句:
for arg in "$@"
do
case $arg in
"--first" )
START_DATE=$arg;;
"--last" )
END_DATE=$arg;;
"--copy" )
COPY=true;;
"--remove" )
REMOVE=true;;
# ... and so on
esac
done
My problem:
我的问题:
that needs a increment $arg+1
type statement to get the following arg (in some cases).
需要一个增量$arg+1
类型语句来获得以下参数(在某些情况下)。
How is that possible?
这怎么可能?
I'm also happy to do a substring such .script --first2012-12-25 --last2012-12-26
我也很乐意做一个子串,例如 .script --first2012-12-25 --last2012-12-26
and not sure how to proceed there.
并且不确定如何在那里进行。
采纳答案by Gabe Rainbow
getopts cannot have optional arguments it seems. otherwise great.
getopts 似乎不能有可选参数。否则很棒。
my solution
我的解决方案
loop the $@
and setting a variable equal to x=$arg
do the case switch on that variable (rather than arg)
循环$@
并设置一个变量等于x=$arg
对该变量(而不是 arg)进行大小写切换
that worked fine for arguments of the type --startdate 2012-12-25 --enddate 2012-12-29
这适用于该类型的参数 --startdate 2012-12-25 --enddate 2012-12-29
but did not work for --remove
that has no following argument.
但没有工作--remove
,没有以下论点。
therefore tack on stuff (unlikely argument) onto the arg string.
因此将东西(不太可能的参数)添加到 arg 字符串上。
leaving the following
留下以下
argc="$@ jabberwhocky"
echo $argc
x=0
# x=0 for unset variable
for arg in $argc
do
case $x in
"--start" )
STARTDATE=$arg ;;
"--end" )
ENDDATE=$arg ;;
"--copy" )
COPY=true;;
"--remove" )
REMOVE=true;;
... and so on....
... 等等....
esac
x=$arg
done
回答by Gilbert
You can allow both --a=arg or -a arg options with a little more work:
你可以允许 --a=arg 或 -a arg 选项多做一点工作:
START_DATE="$(date '+%Y-%m-%d')"; LAST_DATE="$(date '+%Y-%m-%d')"; while [[ $# -gt 0 ]] && [[ "" == "--"* ]] ; do opt=""; shift; #expose next argument case "$opt" in "--" ) break 2;; "--first" ) START_DATE=""; shift;; "--first="* ) # alternate format: --first=date START_DATE="${opt#*=}";; "--last" ) LAST_DATE=""; shift;; "--last="* ) LAST_DATE="${opt#*=}";; "--copy" ) COPY=true;; "--remove" ) REMOVE=true;; "--optional" ) OPTIONAL="$optional_default";; #set to some default value "--optional=*" ) OPTIONAL="${opt#*=}";; #take argument *) echo >&2 "Invalid option: $@"; exit 1;; esac done
Note the --optional argument uses a default value if "=" is not used, else it sets the value in the normal way.
请注意,如果未使用“=”,则 --optional 参数使用默认值,否则它以正常方式设置值。
回答by favoretti
Use shift
in the end of each case
statement.
shift
在每个case
语句的末尾使用。
Quote from a bash
manual:
引用bash
手册:
shift [n]
The positional parameters from n+1 ... are renamed to $1 .... Parameters represented by the numbers $# down to $#-n+1 are unset. n must be a non-negative number less than or equal to $#. If n is 0, no parameters are changed. If n is not given, it is assumed to be 1. If n is greater than $#, the positional parameters are not changed. The return status is greater than zero if n is greater than $# or less than zero; otherwise 0.
移位 [n]
位置参数从 n+1 ... 重命名为 $1 .... 由数字 $# 到 $#-n+1 表示的参数未设置。n 必须是小于或等于 $# 的非负数。如果 n 为 0,则不更改任何参数。如果未给出 n,则假定为 1。如果 n 大于 $#,则不更改位置参数。如果 n 大于 $# 或小于零,则返回状态大于零;否则为 0。
回答by potrzebie
回答by anishsane
$@
is an array, & not a simple variable.
$@
是一个数组,而不是一个简单的变量。
You can capture it to a local variable as x=("$@")
& then use array x with indices as 0 to ($# - 1)
.
您可以将它捕获到局部变量 as x=("$@")
& 然后使用数组 x 和索引 as 0 to ($# - 1)
。
To access individual elements, use ${x[$i]}
. You can NOT directly use ${@[$i]}
, however.
要访问单个元素,请使用${x[$i]}
。但是,您不能直接使用${@[$i]}
。
So instead of for arg in "$@"
loop, you will have i=0; while [ $i -lt $# ]; do
loop.
因此for arg in "$@"
,您将拥有i=0; while [ $i -lt $# ]; do
循环而不是循环。