简体   繁体   English

For 循环在终端中工作但不在 shell 脚本中

[英]For loop working in terminal but not in shell script

I was trying to rename files.我试图重命名文件。

$ls
1.jpg   12.jpg  15.jpg  18.jpg  3.png  6.jpg  9.png
10.jpg  13.jpg  16.jpg  19.jpg  4.jpg  7.png
11.jpg  14.jpg  17.jpg  2.jpg   5.jpg  8.jpg

From 1.jpg to 01.jpg for files {1..9}.* irrespective of extension.对于文件{1..9}.* ,从1.jpg01.jpg ,与扩展名无关。 I tried the following for loop in terminal it worked perfectly.我在终端中尝试了以下for循环,它运行良好。

$for i in {1..9}.*;do mv "$i" 0"$i";done

When I wrote the same in shell script it didn't work.当我在 shell 脚本中编写相同的内容时,它不起作用。

#!/bin/sh
for i in {1..9}.*;do mv "$i" 0"$i";done

It gave the following error它给出了以下错误

mv cannot stat '{1..9}.*':No such file or directory

I figured that posix shell doesn't support {1..9}.* syntax.我认为posix shell 不支持{1..9}.*语法。

Q:what is the equivalent of this in posix?问:在posix中这个等价物是什么?

-Thank You. -谢谢你。

You can use the wildcard pattern [1-9].* .您可以使用通配符模式[1-9].*

#!/bin/sh
for i in [1-9].*
do 
    mv "$i" 0"$i"
done

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

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