繁体   English   中英

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

[英]for loop not working in a shell script

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

语法应为:

对于{11001..110039}中的i; 执行cp / home / usr / BB $ {i} / home / usr /; 做完了

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM