本文介绍了Bash脚本中具有多个条件的for循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
自从我进行了大量的bash脚本编写以来,已经有一段时间了,我忘记了在for循环中执行多个条件的语法.
It's been a while since I've done intense bash scripting and I forgot the syntax for doing multiple conditions in a for loop.
在C语言中,我会这样做:
In C, I'd do:
for(var i=0,j=0; i<arrayOne.length && j<arrayTwo.length; i++,j++){
// Do stuff
}
我已经搜索了一段时间,只发现了涉及嵌套的for循环的语法,而不是涉及到一个for循环的多个条件.
I've been googling for a while and have only found syntax involving nested for loops, not multiple conditions to one for loop.
推荐答案
听起来就像您在谈论算术for循环.
for ((i = j = 0; i < ${#arrayOne[@]} && j < ${#arrayTwo[@]}; i++, j++)); do
# Do stuff
done
假设未设置i
和j
或为零,则大约等于:
Which assuming i
and j
are either unset or zero is approximately equivalent to:
while ((i++ < ${#arrayOne[@]} && j++ < ${#arrayTwo[@]})); do ...
,并且只要您不在乎循环后i
/j
的值,便可以移植一些.
and slightly more portable so long as you don't care about the values of i
/j
after the loop.
这篇关于Bash脚本中具有多个条件的for循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!