perl - 将系统命令重定向到文件
我下面有这个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以通过在终端命令的前面加上
> 来重定向终端命令的输出。 <文件名>。因此:
变成:
You can redirect the output of a terminal command by preceding it with
> <file name>
. Ergo:Becomes:
您似乎对 perl 如何写入文件感到困惑。
MYFILE
句柄上的open
和close
只会创建一个空文件。system
不会在 perl 中对该文件执行任何操作。 (虽然我不能说你的 sh 脚本用它做什么)。一些提示:
@files
的索引而不是元素,这无缘无故地降低了可读性,因为索引本身是
没有在任何地方使用。
split
提取文件名和扩展名,而我假设这是有原因的。不过,没必要设置
限制为 2 个,因为您只使用前一个或两个
来自那次分裂的元素。
\s
)时,不需要括号。因此,请使用
\s
而不是[\s]
。open
和一个词汇文件句柄。
system
仅返回系统调用的返回值。 dunsmoreb是正确的,您可以简单地在 shell 中重定向来创建
文件,但这就是在 perl 中完成的方式。
如果我猜的话,我会说您正在尝试将 bash 脚本的输出写入您尝试打开的文件,在这种情况下您应该这样做:
You seem to be confused with how perl writes to files. Your
open
andclose
on theMYFILE
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:
@files
instead of the elements, whichreduces readability for no reason, since the indexes themselves are
not used anywhere.
split
to extract file name and extension, and Iassume 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.
\s
), thebrackets are not needed. So, use
\s
instead of[\s]
.open
with alexical file handle.
system
only returns the return value of the system call. dunsmorebis 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: