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

标签 linux perl bash shell unix

我下面有这段 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); 
}

最佳答案

您可以通过在终端命令前加上> <file name> 来重定向终端命令的输出。 .因此:

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

变成:

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

关于linux - perl - 将系统命令重定向到文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8691915/

相关文章:

linux - 如何在 Linux 中就地解压文件

mysql - DBI/DBD::mysql/mysql_type_name: 如何区分Blob和Text?

bash - echo "-n"不会打印-n?

macos - latexindent:自 macOS 11 Big Sur 以来安装 File::HomeDir、Mac::SystemDirectory 时遇到问题

linux - 将 key 发送到终端

bash - 如何在 ffmpeg 中创建随机通行证名称?

linux - 如何让 2 个变量具有 2 个不同的序列 - Bash Linux

linux - Linux的Karabiner?

linux - 奇怪的递归目录结构

perl - 如何使用 MIME::Lite perl 检查上次发送的电子邮件是否已成功发送