从 Perl 执行命令行命令?

发布于 2024-09-30 21:24:22 字数 457 浏览 7 评论 0原文

大家好,

我需要从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

情绪操控生活 2024-10-07 21:24:22

我可以想到许多更好的方法来做到这一点,但理想情况下你想要纯 Perl:

use File::Copy qw( move );
opendir DIR, $mov0to or die "Unable to open $mov0to: $!";
foreach my $file ( readdir DIR ) {
    my $out = $file;
    next unless $out =~ s/\.jp4$/.jpg/;
    move "$mov0to/$file", "$basedir/internal/0/$out" or die "Unable to move $mov0to/$file->$basedir/internal/0/$out: $!";
}
closedir DIR;

如果你坚持在 Bash 中进行提升,你应该这样做:

system qq(bash -c 'for file in $mov0to/*.jp4 ; do mv \$file $basedir/internal/0/\${file\/%.jp4\/.jpg} ; done');

I can think of many better ways of doing this, but ideally you want pure Perl:

use File::Copy qw( move );
opendir DIR, $mov0to or die "Unable to open $mov0to: $!";
foreach my $file ( readdir DIR ) {
    my $out = $file;
    next unless $out =~ s/\.jp4$/.jpg/;
    move "$mov0to/$file", "$basedir/internal/0/$out" or die "Unable to move $mov0to/$file->$basedir/internal/0/$out: $!";
}
closedir DIR;

If you insist on doing the lifting in Bash, you should be doing:

system qq(bash -c 'for file in $mov0to/*.jp4 ; do mv \$file $basedir/internal/0/\${file\/%.jp4\/.jpg} ; done');
吃→可爱长大的 2024-10-07 21:24:22

双引号内的所有变量都将被插入,特别是您在 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.

世界如花海般美丽 2024-10-07 21:24:22

这很可能是嵌套引号的问题。我建议要么将该 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文