2014-05-05 134 views
0

我已经一直使用以下格式的“for循环”在shell脚本:for循环在shell脚本不工作

例如:

for i in {11001..110039} 
do 
cp /home/usr/BB${i} /home/usr/ 
现在

,我得到了以下错误:

/home/usr/BB{11001..11039} does not exist 

应该考虑的所有文件BB11001到BB11039,它总是这样工作的,现在我不知道为什么我会得到这个错误。 有什么帮助吗?

+0

是语法对吗?如果这是我的话;做 \t#陈述 done'? – scorpiozj

+0

是的语法就像你写的 – infoSyStem

+0

那么/ home/usr/BB11001是否存在? – scorpiozj

回答

2

您可能使用/ bin/sh而不是/ bin/bash。

编辑#1(POC):

$ bash --version 
GNU bash, version 4.2.45(1)-release (x86_64-pc-linux-gnu) 
Copyright (C) 2011 Free Software Foundation, Inc. 
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html> 

This is free software; you are free to change and redistribute it. 
There is NO WARRANTY, to the extent permitted by law. 

$ cat t.sh 
#!/bin/bash 

for i in {1..5} 
do 
echo $i 
done 
$ ./t.sh 
1 
2 
3 
4 
5 
$ 

并与/ bin/sh的:

$ cat t.sh 
#!/bin/sh 

for i in {1..5} 
do 
echo $i 
done 
$ ./t.sh 
{1..5} 
$ 
+0

你可以提供你的bash版本(如果你真的使用bash)? – pah

+0

GNU bash version 4.1.5 – infoSyStem

+0

我不确定在哪个版本中引入了该语法,但v4.1.5似乎已经足以实现它了。 – pah

1

的语法应该是:

对于i {11001..110039} ; do cp/home/usr/BB $ {i}/home/usr /; done

+0

是的,我做到了这一点 – infoSyStem

+0

这是无关紧要的。 OP语法正确,只要他在cp – pah

+0

之后使用'done'语句在第一个示例中“完成”丢失。 – rubenafo