bash 只有偶数的数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7100473/
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
Array with only even number
提问by LookIntoEast
for X in {18..2500} ;is one line of my script, which means to pick number one by one like: 18,19,20,21,22,23....till 2500
for X in {18..2500} ;是我的脚本中的一行,这意味着逐个选择数字,例如:18,19,20,21,22,23....till 2500
However I find I only need even number right now: 18,20,22,24.....2500
但是我发现我现在只需要偶数:18,20,22,24.....2500
Then what should I do by a slight modify of the line?
那么我应该通过稍微修改线路来做什么?
Thanks
谢谢
edit: It's bash...
编辑:这是bash ...
My script is now changed to:
我的脚本现在更改为:
#!/bin/bash
TASK=1101;
NUM=9;
TEND=1100;
for X in {18..2500};{
if (X % 2 == 0);
do
echo "$X echo \"Wait until $NUM job is done\" $NUM" ;
NUM=$((NUM+2)) ;
X=$((X+1)) ;
TEND=$((TEND+100)) ;
echo "$X -t $TASK-$TEND jobs.sh" ;
TASK=$((TASK+100)) ;}
done
but got errors like: line 15: syntax error near unexpected token `do'
但得到了如下错误:第 15 行:意外标记“do”附近的语法错误
回答by Karoly Horvath
You can specify the increment:
您可以指定增量:
for X in {18..2500..2}
A sequence expression takes the form {x..y[..incr]}, where x and y are
either integers or single characters, and incr, an optional increment, is
an integer.
Or
或者
for X in `seq 18 2 2500`
回答by Johnsyweb
This is not C++. This is a bash script.
这不是 C++。这是一个 bash 脚本。
Your for-loop needs to start with a do:
您的for-loop 需要以一个开头do:
for X in {18..2500}; do
Your if-statement syntaxlooks off. It should probably be something like this, note the then:
您的if-statement 语法看起来很糟糕。它可能应该是这样的,注意then:
if [[ $((X % 2)) == 0 ]]; then
if-blocks end with:
if-blocks 以:
fi
And the for-doblock ends with:
和for-do块结束时有:
done
Better still... do away with the ifstatement and use Bash's for-loop constructto generate only even numbers:
更好的是......取消该if语句并使用 Bash 的for-loop 构造仅生成偶数:
for ((X = 18; X <= 2500; X += 2)); do
echo "$X echo \"Wait until $NUM job is done\" $NUM" ;
# ...
done
回答by DaveRandom
If your language has a for(;;)syntax you can
如果您的语言有for(;;)语法,您可以
for (X = 18; X <= 2500; X += 2)
回答by FeifanZ
Try the modulus operator. In almost all languages, it'll look something like this:
试试模运算符。在几乎所有语言中,它看起来像这样:
if (x % 2 == 0) // …Do something
That is valid C code, but can easily be applied to other languages.
这是有效的 C 代码,但可以很容易地应用于其他语言。
You can think of the mod operator as a division sign placed in the same location, but rather than returning the results of the division it returns the remainder. Therefore in this code, if the remainder of a divide-by-two is 0, then it divides evenly by two, and so it's even by definition.
您可以将 mod 运算符视为放置在同一位置的除法符号,但它不返回除法的结果,而是返回余数。因此,在这段代码中,如果被二除的余数为 0,则它被二整除,因此根据定义它是偶数。
回答by sigil
There are a couple things you can do:
您可以做以下几件事:
- use the modulus function for your language:
- 为您的语言使用模数函数:
for x in {18..2500} {
对于 {18..2500} 中的 x {
if (x mod 2=0) {
如果(x mod 2 = 0){
do something;}
做点什么;}
- step through your For loop 2 at a time:
- 一次单步执行 For 循环 2:
for x in {18..2500} step 2 {
对于 {18..2500} 中的 x 步骤 2 {
do something;}
做点什么;}

