bash 脚本 - 无法让循环工作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22863783/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 10:07:29  来源:igfitidea点击:

bash script - can't get for loop working

linuxbashfor-loop

提问by dot

Background Info:

背景资料:

I'm trying to follow the example posted here: http://www.cyberciti.biz/faq/bash-for-loop/I would like loop 9 times using a control variable called "i".

我正在尝试按照此处发布的示例进行操作:http: //www.cyberciti.biz/faq/bash-for-loop/我想使用名为“i”的控制变量循环 9 次。

Problem Description

问题描述

My code looks like this:

我的代码如下所示:

for i in {0..8..1}
do
  echo "i is $i"
  tmpdate=$(date -d "$i days" "+%b %d")
  echo $tmpdate
done

When I run this code, the debug prints show me:

当我运行此代码时,调试打印显示:

                 "i is {0..8..1}" 

instead of being a value between 0 and 8.

而不是介于 0 和 8 之间的值。

What I've Checked So Far:

到目前为止我检查过的内容:

I've tried to check my version of bash to make sure it supports this type of syntax. I'm running version 4,2,25(1)

我试图检查我的 bash 版本以确保它支持这种类型的语法。我正在运行版本 4,2,25(1)

I also tried using C like syntax where you do for (i=0;i<=8;i++) but that doesn't work either.

我也试过在你为 (i=0;i<=8;i++) 做的地方使用类似 C 的语法,但这也不起作用。

Any suggestions would be appreciated.

任何建议,将不胜感激。

Thanks.

谢谢。

EDIT 1

编辑 1

I've also tried the following code:

我也试过下面的代码:

for i in {0..8};
do
  echo "i is $i"
  tmpdate=$(date -d "$i days" "+%b %d")
  echo $tmpdate
done

And...

和...

for i in {0..8}
do
  echo "i is $i"
  tmpdate=$(date -d "$i days" "+%b %d")
  echo $tmpdate
done

They all fail with the same results.

它们都以相同的结果失败。

I also tried:

我也试过:

#!/bin/bash

for ((i=0;i<9;i++));
do
  echo "i is $i"
  tmpdate=$(date -d "$i days" "+%b %d")
  echo $tmpdate
done

And that gives me the error:

这给了我错误:

test.sh: 4: test.sh: Syntax error: Bad for loop variable

test.sh: 4: test.sh: 语法错误:循环变量错误

FYI. I'm running on ubuntu 12

供参考。我在 ubuntu 12 上运行

EDIT 2

编辑 2

Ok... so i think Weberick tipped me off to the issue... To execute the script, I was running "sh test.sh" when in the code I had defined it as a BASH script! My bad!

好的...所以我认为 Weberick 向我提示了这个问题...为了执行脚本,我在代码中将它定义为 BASH 脚本时运行了“sh test.sh”!我的错!

But here's the thing. Ultimately, I need it to work in both bash and sh. so now that I'm being careful to make sure that I invoke the script the right way... I've noticed the following results:

但事情是这样的。最终,我需要它同时在 bash 和 sh 中工作。所以现在我很小心地确保我以正确的方式调用脚本......我注意到以下结果:

  1. when defined as a bash script and i execute using bash, the C-style version works!
  2. when defined as an sh script and i execute using sh, the C-style version fails

    me@devbox:~/tmp/test$ sh test.sh test.sh: 5: test.sh: Syntax error: Bad for loop variable

  3. when defined as an sh script and i execute using sh the NON c style version ( aka for i in {n ..x}), I get the "i is {0..8}" output.

  1. 当定义为 bash 脚本并且我使用 bash 执行时,C 样式版本有效!
  2. 当定义为 sh 脚本并且我使用 sh 执行时,C 样式版本失败

    me@devbox:~/tmp/test$ sh test.sh test.sh: 5: test.sh: 语法错误: 循环变量错误

  3. 当定义为 sh 脚本并且我使用 sh 非 c 样式版本(又名 for i in {n ..x})执行时,我得到“i is {0..8}”输出。

PS. The ";" doesn't make a difference if you have the do on the next line...just FYI.

附注。这 ”;” 如果你在下一行做,没有什么区别......仅供参考。

采纳答案by Josh Jolly

Ubuntu's default shell is dash, which doesn't recognise either of the bashisms (brace expansion, C-style for loop) you tried. Try running your script using bashexplicitly:

Ubuntu 的默认 shell 是dash,它无法识别您尝试过的任何一种 bashisms(大括号扩展、C 风格的 for 循环)。尝试使用bash显式运行脚本:

bash myscript.sh

or by setting the shebang to #!/bin/bash. Make sure NOT to run the script with sh myscript.sh.

或者通过将shebang设置为#!/bin/bash. 确保不要使用sh myscript.sh.

dash should work if you use seq:

如果您使用 dash 应该可以工作seq

for i in $(seq 0 1 8); do
    echo "$i"
done

Just {0..8}should work in bash, the default increment is 1. If you want to use a C-style for loop in bash:

只是{0..8}应该工作在bash,默认增量为1。如果你想使用一个C风格中循环bash

for ((i=0;i<9;i++)); do 
    echo "$i"
done

回答by Jessada Thutkawkorapin

I'm confident that

我有信心

#!/bin/bash

for ((i=0;i<9;i++))
do
  echo "i is $i"
  tmpdate=$(date -d "$i days" "+%b %d")
  echo $tmpdate
done

work on Ubuntu 12.04

在 Ubuntu 12.04 上工作

If you still have an error, can you please try running

如果您仍然有错误,请您尝试运行

chmod +x test.sh

then

然后

./test.sh

And the result is

结果是

i is 0
Apr 04
i is 1
Apr 05
i is 2
Apr 06
i is 3
Apr 07
i is 4
Apr 08
i is 5
Apr 09
i is 6
Apr 10
i is 7
Apr 11
i is 8
Apr 12

回答by Stephan Kulla

The site you quote says

你引用的网站说

Bash v4.0+ has inbuilt support for setting up a step value using {START..END..INCREMENT} syntax:

Bash v4.0+ 内置支持使用 {START..END..INCREMENT} 语法设置步长值:

So you can just use {0..8..1}when you have a bash version greater than 4.0, which I guess is not the case (try bash --versionin your terminal). Instead of {0..8..1}you can also use {0..8}.

因此,{0..8..1}当您的 bash 版本大于 4.0 时,您就可以使用,我想情况并非如此(bash --version在您的终端中尝试)。相反的{0..8..1},你也可以使用{0..8}

If you have an older version you can use instead of {START..END..INCREMENT}the command $(seq START INCREMENT END)in the for loop.

如果您有旧版本,则可以使用代替for 循环中{START..END..INCREMENT}的命令$(seq START INCREMENT END)

回答by cheezsteak

I'm no expert at bash but according to tdlpyou need a ;after the for statement. There are many ways to to a range. This is one of them.

我不是 bash 专家,但根据tdlp,您需要;在 for 语句之后添加一个。有很多方法可以达到一个范围。这是其中之一。

    #!/bin/bash
    for i in `seq 1 8`; do
        echo $i
    done