bash脚本 - 不能得到循环工作 [英] bash script - can't get for loop working

查看:114
本文介绍了bash脚本 - 不能得到循环工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景资料:

我试图按照张贴在这里的例子: HTTP:// WWW。 cyberciti.biz/faq/bash-for-loop/
我想循环中使用所谓的我控制变量9倍。

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".

问题说明

我的code是这样的:

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

当我运行这个code,调试打印告诉我:

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

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

,而不是为0和8之间的值

instead of being a value between 0 and 8.

什么,我经过这么远:

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

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)

我用C一样,你对做语法也试过(; I< I = 0 = 8;我+ +),但也不起作用。

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

任何建议将是AP preciated。

Any suggestions would be appreciated.

感谢。

修改1

我也试过以下code:

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

和...

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.

我也试过:

#!/bin/bash

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

这给我的错误:

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

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

仅供参考。我在Ubuntu上运行12

FYI. I'm running on ubuntu 12

编辑2

好吧......所以,我认为Weberick通风报信我送行的问题...
要执行脚本,我跑的sh test.sh
当在code我有它定义为一个bash脚本!我的坏!

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!

但这里的东西。最后,我需要在这两个bash和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:


  1. 当定义为一个bash脚本和我执行使用bash,C风格的版本的作品!

  2. 当定义为一个sh脚本,我使用执行SH,C风格的版本失败

  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

我@ devbox:〜/ tmp / test目录$ SH test.sh
test.sh:5:test.sh:语法错误:坏的循环变量

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

当定义为一个sh脚本,我使用执行的SH非空调风格版(又名为我{N ..x}),我得到了我是{} 0..8输出。

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.

PS。的,不有所作为,如果你对下一行做...只是供参考。

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

推荐答案

Ubuntu的默认外壳是破折号,不承认无论是bash化的(括号扩展,C风格的for循环),你试过。尝试使用运行脚本庆典明确:

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 bash explicitly:

bash myscript.sh

或由家当设置为#!/斌/庆典。确保不要运行该脚本 SH myscript.sh

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

如果您使用破折号应该工作 SEQ

dash should work if you use seq:

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

只是 {0..8} 庆典工作,默认增量为1。如果你想使用C风格的for循环庆典

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

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

这篇关于bash脚本 - 不能得到循环工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆