我一直在 shell 脚本中使用以下格式作为“for 循环”:
例子:
for i in {11001..110039}
do
cp /home/usr/BB${i} /home/usr/
现在,我收到以下错误:
/home/usr/BB{11001..11039} does not exist
它应该考虑所有文件 BB11001 到 BB11039,它一直这样工作,现在我不知道为什么会出现此错误。 有什么帮助吗?
最佳答案
您可能正在使用/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}
$
关于linux - for循环在shell脚本中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23470834/