仅包含偶数的数组

发布于 2024-11-30 12:30:07 字数 592 浏览 0 评论 0 原文

for X in {18..2500} ; 是我脚本的一行,这意味着逐一选择数字,例如:18,19,20,21,22,23....直到2500

但是我发现我现在只需要偶数:18,20,22,24.....2500

那么我应该稍微修改一下该行怎么办?

感谢

编辑: 它是 bash...

我的脚本现在更改为:

#!/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 

但出现如下错误: 第 15 行:意外标记“do”附近出现语法错误

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

However I find I only need even number right now: 18,20,22,24.....2500

Then what should I do by a slight modify of the line?

Thanks

edit:
It's 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'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

思念绕指尖 2024-12-07 12:30:07

您可以指定增量:

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.

或者

for X in `seq 18 2 2500`

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`
唯憾梦倾城 2024-12-07 12:30:07

这不是 C++。这是一个 bash 脚本。

您的 for 循环需要以 do 开头:

for X in {18..2500}; do

您的 if-语句语法看起来不对。它可能应该是这样的,注意then

    if [[ $((X % 2)) == 0 ]]; then

if-blocks以:

    fi

for-do 块结尾为:

done

更好的是...取消 if 语句并使用 Bash 的 for-循环构造 仅生成偶数:

for ((X = 18; X <= 2500; X += 2)); do
     echo "$X      echo \"Wait until $NUM job is done\" $NUM" ;
     # ... 
done

This is not C++. This is a bash script.

Your for-loop needs to start with a do:

for X in {18..2500}; do

Your if-statement syntax looks off. It should probably be something like this, note the then:

    if [[ $((X % 2)) == 0 ]]; then

if-blocks end with:

    fi

And the for-do block ends with:

done

Better still... do away with the if statement and use Bash's for-loop construct to generate only even numbers:

for ((X = 18; X <= 2500; X += 2)); do
     echo "$X      echo \"Wait until $NUM job is done\" $NUM" ;
     # ... 
done
☆獨立☆ 2024-12-07 12:30:07

尝试模数运算符。在几乎所有语言中,它看起来都是这样的:

if (x % 2 == 0)    // …Do something

这是有效的 C 代码,但可以轻松应用于其他语言。

您可以将 mod 运算符视为放置在同一位置的除法符号,但它不返回除法结果,而是返回余数。因此,在这段代码中,如果除以二的余数为 0,则它能被二整除,因此根据定义它是偶数。

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.

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.

早乙女 2024-12-07 12:30:07

如果您的语言有 for(;;) 语法,您可以

for (X = 18; X <= 2500; X += 2)

If your language has a for(;;) syntax you can

for (X = 18; X <= 2500; X += 2)
热情消退 2024-12-07 12:30:07

您可以执行以下操作:

  1. 使用适合您语言的模函数:

for x in {18..2500} {

if (x mod 2=0) {

do some;}

  1. 一次执行 For 循环 2:

对于 {18..2500} 步骤 2 中的 x {

做某事;}

There are a couple things you can do:

  1. use the modulus function for your language:

for x in {18..2500} {

if (x mod 2=0) {

do something;}

  1. step through your For loop 2 at a time:

for x in {18..2500} step 2 {

do something;}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文