bash 脚本中的正则表达式(for 循环)
我想通过使用 for 循环来解析提供给 shell 脚本的参数。现在,假设我有 3 个参数,比如 对于我来说,$1 $2 $3
应该完成这项工作,但我无法预测参数的数量,所以我想使用正则表达式作为范围,并使用 $# 作为最后一个参数的数量。我不知道如何在 for 循环中使用这些 RegEx',我尝试了类似的方法 对于 $[1-$#] 中的 i
这是行不通的。该循环仅运行 1 次,并且正在计算 1-$#,而不是用作正则表达式。
I want to parse the arguments given to a shell script by using a for-loop. Now, assuming I have 3 arguments, something likefor i in $1 $2 $3
should do the job, but I cannot predict the number of arguments, so I wanted use an RegEx for the range and $# as the number of the last argument. I don't know how to use these RegEx' in a for-loop, I tried something likefor i in $[1-$#]
which doesn't work. The loop only runs 1 time and 1-$# is being calculated, not used as a RegEx.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
基本
如果您不指定
in
子句,默认情况下,for
循环将循环遍历命令行参数:如果您想明确,您可以获取所有参数为
"$@"
。上面的循环相当于:从 bash 手册页:
高级
对于重型参数处理,
getopt
+shift
是最佳选择。getopt
将预处理命令行,为用户指定参数的方式提供一定的灵活性。例如,它将-xzf
扩展为-x -z -f
。它在所有标志后面添加一个--
参数,用于将标志与文件名分开;这可以让你运行cat -- -my-file
来显示-my-file
的内容,而不会在前导破折号上呕吐。尝试使用此样板代码来了解大小:
请注意,每个选项都有一个短的和一个长的等效项,例如
-a
和--alpha
。-a
标志接受一个参数,因此在getopt
调用中将其指定为a:
和alpha:
,并且在其大小写末尾有一个shift 2
。Basic
A
for
loop by default will loop over the command-line arguments if you don't specify thein
clause:If you want to be explicit you can get all of the arguments as
"$@"
. The above loop is equivalent to:From the bash man page:
Advanced
For heavy-duty argument processing,
getopt
+shift
is the way to go.getopt
will pre-process the command-line to give the user some flexibility in how arguments are specified. For example, it will expand-xzf
into-x -z -f
. It adds a--
argument after all the flags which separates flags from file names; this lets you do runcat -- -my-file
to display the contents of-my-file
without barfing on the leading dash.Try this boilerplate code on for size:
Notice that each option has a short a long equivalent, e.g.
-a
and--alpha
. The-a
flag takes an argument so it's specified asa:
andalpha:
in thegetopt
call, and has ashift 2
at the end of its case.另一种迭代参数的方法更接近您正在努力的方向,类似于:
但 John Kugelman 展示的 for arg 语法是迄今为止更可取的。然而,有时数组切片很有用。此外,在这个版本中,与约翰的版本一样,参数数组保持不变。使用
shift
会丢弃其元素。您应该注意,您尝试使用方括号执行的操作根本不是正则表达式。
Another way to iterate over the arguments which is closer to what you were working toward would be something like:
but the
for arg
syntax that John Kugelman showed is by far preferable. There are, however, times when array slicing is useful. Also, in this version, as in John's, the argument array is left intact. Usingshift
discards its elements.You should note that what you were trying to do with square brackets is not a regular expression at all.
我建议做其他事情:
shift
关键字将 $2 的内容移动到 $1,将 $3 移动到 $2,等等。 pp.让我们说一下参数:
在
shift
之后,现在的参数是:通过 while 循环,您可以解析任意数量的参数,甚至可以执行以下操作:
将参数想象为一个数组,而 $n 是该数组的索引。
shift
删除第一个元素,因此索引 1 现在引用shift
之前索引 2 处的元素。我希望你明白我想说的话。I suggest doing something else instead:
The
shift
keyword moves the content of $2 into $1, $3 into $2, etc. pp.Let's say the arguments where:
After a
shift
, the arguments are now:With the while loop, you can thus parse an arbitrary number of arguments and can even do things like:
Imagine the arguments as being an array and $n being indexes into that array.
shift
removes the first element, so the index 1 now references the element that was at index 2 prior toshift
. I hope you understand what I want to say.