linux - for循环在shell脚本中不起作用

标签 linux shell loops for-loop

我一直在 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/

相关文章:

php - 如何在 Dreamhost 上为多个域创建共享文件夹

c++ - 在 C++ 中使用 'deque' 的无限循环

c - Hook 在终端上。我可以在终端中运行命令之前调用方法吗?

java - 编写一个程序,计算 (a/1)+(a/2)+(a/3)+(a/4)+........+(a/n) 之和

loops - 如何从外循环 : 中找到依赖于 "i"的内循环的时间复杂度

c - Linux 如何知道要调用哪个 ioctl 函数?

linux - 如何更改 GCC 版本

c++ - 如何从我的 Windows 笔记本电脑轻松设计基于 Linux 的终端应用程序?

shell - 如何在shell中创建日期生成器?

bash - 在heredoc中禁用命令替换?