从 Perl 执行命令行命令?
大家好,
我需要从 Perl 文件执行这个命令行命令:
for file in *.jpg ; do mv $file `echo $file | sed 's/\(.*\.\)jpg/\1jp4/'` ; done
所以我添加了文件夹并尝试:
system "bash -c 'for file in $mov0to/*.jp4 ; do mv $file `echo $basedir/internal/0/$file | sed 's/\(.*\.\)jp4/\1jpg/'` ; done'";
但我得到的是:
sh: Syntax error: "(" unexpected
No file specified
我在 Kubuntu 10.4
谢谢, 扬
HI all,
i need to have this command line command executed from a Perl file:
for file in *.jpg ; do mv $file `echo $file | sed 's/\(.*\.\)jpg/\1jp4/'` ; done
So I added the folders and tried:
system "bash -c 'for file in $mov0to/*.jp4 ; do mv $file `echo $basedir/internal/0/$file | sed 's/\(.*\.\)jp4/\1jpg/'` ; done'";
But all I get is:
sh: Syntax error: "(" unexpected
No file specified
I am on Kubuntu 10.4
Thanks,
Jan
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我可以想到许多更好的方法来做到这一点,但理想情况下你想要纯 Perl:
如果你坚持在 Bash 中进行提升,你应该这样做:
I can think of many better ways of doing this, but ideally you want pure Perl:
If you insist on doing the lifting in Bash, you should be doing:
双引号内的所有变量都将被插入,特别是您在 bash for 循环中使用的
$file
。我们没有足够的上下文来了解其他变量($mov0to
和$basedir
)来自何处或它们包含什么,因此它们可能会遇到相同的问题。双引号也会吃掉 sed 部分中的反斜杠。Andy White 是对的,如果 bash 命令位于单独的脚本中,那么混乱就会少一些。
All of the variables inside the double quotes will get interpolated, the
$file
that you're using in the bash for loop in particular. We don't have enough context to know where the other variables ($mov0to
and$basedir
) come from or what they contain so they might be having the same problems. The double quotes are also eating your backslashes in the sed part.Andy White is right, you'd have less of a mess if the bash commands were in a separate script.
这很可能是嵌套引号的问题。我建议要么将该 bash 命令移至其自己的 (bash) 脚本文件中,要么直接在 Perl 代码中编写循环/逻辑,以便在需要时进行更简单的系统调用。
It's most likely a problem with nested quotes. I would recommend either moving that bash command into its own (bash) script file, or writing the loop/logic directly in Perl code, making simpler system calls if you need to.