perl - 将系统命令重定向到文件

发布于 2024-12-24 19:31:32 字数 594 浏览 0 评论 0原文

我下面有这个 perl 代码,基本上是想循环这两个文件。然后调用 bash 脚本(它将文件和分隔符作为参数,并打印出多行),然后将该命令的所有输出保存到一个新文件中(文件名末尾添加“_processed”)。 下面的语法有什么问题(目前它只是创建一个空白文件)?

#!/usr/bin/env perl
use strict;
use warnings;

my(@files) = (
"fixedtt.txt '|'",
"delimd2iffpipe.dat '|'"
);
for my $i (0 .. $#files) {
    my($filename) = split /[\s]+/, "$files[$i]", 2;
    my($name, $type) = split /[\.]+/, "$filename", 2;
    open (MYFILE, '>'."$name".'_processed.'."$type");
    system("sh myBashScript.sh $files[$i]");   #I want to write the entire output of this command to a file
    close (MYFILE); 
}

I have this perl code below, basically want to loop through these 2 files. Then call a bash script (it takes the file and a delimiter as arguments and it prints out multiple lines) and then save all output from that command into a new file (with filename having '_processed' tacked on the end).
What is wrong with the below syntax (at the moment it just creates a blank file)?

#!/usr/bin/env perl
use strict;
use warnings;

my(@files) = (
"fixedtt.txt '|'",
"delimd2iffpipe.dat '|'"
);
for my $i (0 .. $#files) {
    my($filename) = split /[\s]+/, "$files[$i]", 2;
    my($name, $type) = split /[\.]+/, "$filename", 2;
    open (MYFILE, '>'."$name".'_processed.'."$type");
    system("sh myBashScript.sh $files[$i]");   #I want to write the entire output of this command to a file
    close (MYFILE); 
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

挽清梦 2024-12-31 19:31:32

您可以通过在终端命令的前面加上 > 来重定向终端命令的输出。 <文件名>。因此:

system("sh myBashScript.sh $files[$i]");

变成:

system("sh myBashScript.sh $files[$i] > file.txt");

You can redirect the output of a terminal command by preceding it with > <file name>. Ergo:

system("sh myBashScript.sh $files[$i]");

Becomes:

system("sh myBashScript.sh $files[$i] > file.txt");
谷夏 2024-12-31 19:31:32

您似乎对 perl 如何写入文件感到困惑。 MYFILE 句柄上的 openclose 只会创建一个空文件。 system 不会在 perl 中对该文件执行任何操作。 (虽然我不能说你的 sh 脚本用它做什么)。

一些提示:

  • 您正在使用 @files 的索引而不是元素,这
    无缘无故地降低了可读性,因为索引本身是
    没有在任何地方使用。
  • 您正在使用 split 提取文件名和扩展名,而我
    假设这是有原因的。不过,没必要设置
    限制为 2 个,因为您只使用前一个或两个
    来自那次分裂的元素。
  • 当您只有一个字符(或字符类,例如 \s)时,
    不需要括号。因此,请使用 \s 而不是 [\s]
  • 建议使用三参数 open 和一个
    词汇文件句柄。
  • 捕获输出(到标准输出)是通过反引号或qx()完成的。
    system 仅返回系统调用的返回值。 dunsmoreb
    是正确的,您可以简单地在 shell 中重定向来创建
    文件,但这就是在 perl 中完成的方式。

如果我猜的话,我会说您正在尝试将 bash 脚本的输出写入您尝试打开的文件,在这种情况下您应该这样做:

my @files = (
    "fixedtt.txt '|'",
    "delimd2iffpipe.dat '|'"
);
for my $args (@files) {
    my ($filename)    = split ' ', $args;
    $filename =~ s/\./_processed./;
    open my $fh, '>', $filename or die $!;
    print $fh qx(sh myBashScript.sh $args);
    close $fh; 
}

You seem to be confused with how perl writes to files. Your open and close on the MYFILE handle will only create an empty file. system does not do anything to that file within perl. (Though I cannot say what your sh-script does with it).

Some pointers:

  • You are using the indexes of @files instead of the elements, which
    reduces readability for no reason, since the indexes themselves are
    not used anywhere.
  • You are using split to extract file name and extension, and I
    assume there's a reason for that. However, there's no need to set
    LIMIT to 2, since you are only ever using the first one or two
    elements from that split.
  • When you only have one character (or character class, e.g. \s), the
    brackets are not needed. So, use \s instead of [\s].
  • It is recommended to use the three-argument open with a
    lexical file handle.
  • Capturing output (to stdout) is done with backticks or qx().
    system only returns the return value of the system call. dunsmoreb
    is correct that you can simply redirect in the shell to create the
    file, but this is how it's done inside perl.

If I were to guess, I'd say you were trying to write the output of the bash script to the file you are attempting to open, in which case you should do:

my @files = (
    "fixedtt.txt '|'",
    "delimd2iffpipe.dat '|'"
);
for my $args (@files) {
    my ($filename)    = split ' ', $args;
    $filename =~ s/\./_processed./;
    open my $fh, '>', $filename or die $!;
    print $fh qx(sh myBashScript.sh $args);
    close $fh; 
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文