在bash for循环中定义范围不起作用 [英] Defining ranges in bash for loops didn't work

查看:210
本文介绍了在bash for循环中定义范围不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在bash中有以下循环,在chrooted目录中创建循环设备。

  for var in 0 1 2 3..7 
do
MAKEDEV -d $ {chroot} / dev -x loop $ var
done

这对我来说不起作用,因为在它创建loop3之后,它需要..从字面上,尝试创建循环..并失败。但根据这个教程,它应该已经工作了。 ($ var $ 0; var <= 7; var ++))
do
MAKEDEV -d $ {chroot} / dev -x循环$ var
完成

我仍然想知道为什么我首先尝试的for循环没有工作。请帮助。

解决方案

我认为你是从字面上阅读教程。 1 2 3 4 5 .. N

 对于VARIABLE  

但从字面上看,这是不正确的bash语法 - 我认为作者只是试图说,对于这种循环你需要明确列出你需要迭代的所有值。所以在你的情况下,这将是:

  for var in 0 1 2 3 4 5 6 7 

本教程还提到了括号扩展,@glennjackman在他的回答中给出了这也是完全正确的语法。 b

I have the following for loop in bash that creates loop devices in a chrooted directory.

for var in 0 1 2 3 .. 7
do
    MAKEDEV -d ${CHROOT}/dev -x loop$var
done

This didn't work for me as after it creates loop3 it takes .. literally and tries to create loop.. and fails. However according to this tutorial it should have worked. I got it to work by doing the following:

for (( var=0; var<=7; var++ ))
do
    MAKEDEV -d ${CHROOT}/dev -x loop$var
done

I still want to know why the for loop I tried first didn't work. Please help.

解决方案

I think you are reading the tutorial too literally. The turorial does say

for VARIABLE in 1 2 3 4 5 .. N

but taken literally this is not correct bash syntax - I think the author is simply trying to say that for this kind of for loop you need to explicitly list out all values you need to iterate over. So in your case this would be:

for var in 0 1 2 3 4 5 6 7

The tutorial also mentions brace expansion, which @glennjackman gives in his answer which is also entirely correct syntax.

这篇关于在bash for循环中定义范围不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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