1

在这里排名初学者请温柔...我正在用perl编写一个程序,它可以找到所有特定的文件类型并调用另一个名为newstack的程序来转换文件类型。

当我newstack oldfileame newfilename从我的外壳运行时,它工作正常。

当我的程序运行时system("newstack oldfileame newfilename")newstack 返回错误:

ERROR: NEWSTACK - NO INPUT FILE SELECTED
sh: line1: ./oldfilename: cannot execute binary file

如果我编写一个执行相同操作的 shell 脚本,一次在文件上运行一个 newstack,它就可以正常工作。我在这里遗漏了什么,为什么在 perl 程序的上下文中运行时它会失败?

Newstack来自IMOD程序套件,我不知道它是用什么写的。文件是mrc文件,它们是二进制图像文件。

编辑:: 这是所要求的实际代码:

print "Enter the rootname of the files to be converted: ";
my $filename = <STDIN>;
chop $filename;

my @files = qx(ls $filename*.mrc);     
open LOGFILE, (">modeconvert-log");     
foreach my $mrc (@files)           
{        
print LOGFILE "$mrc";       
system("newstack -mode 2 $mrc $mrc");     
} 
my $fileno = @files;
print "$fileno files converted\n";

chop $mrc在第 8 行之后添加,它解决了问题

4

1 回答 1

2

您发布的代码和您执行的代码不同。在您执行的代码中,后面有一个换行符newstack

$ perl -e'system("who\n oldfileame newfilename")'
sh: line 1: oldfileame: command not found

chomp($x)使用或 using删除换行符$x =~ s/\s+\z//;


my @files = qx(ls $filename*.mrc);

应该

my @files = qx(ls $filename*.mrc);
chomp @files;

或者更好:

my @files = glob("\Q$filename\E*.mrc");

上述和其他修复:

use IPC::System::Simple qw( system );                          # Replaces system with one that dies on Checks for errors.

open(my $LOGFILE, '>', 'modeconvert-log')                      # Avoids global vars.
   or die("Can't create log file \"modeconvert-log\": $!\n");  # Adds useful error checking.

print "Enter the rootname of the files to be converted: ";
my $filename = <STDIN>;
chomp $filename;                                               # chomp is safer.

my @files = glob("\Q$filename\E*.mrc");                        # Works with file names with spaces, etc.

for my $mrc (@files) {
   print $LOGFILE "$mrc\n";                                    # Was missing a newline.
   system("newstack", "-mode", "2", $mrc, $mrc);               # Works with file names with spaces, etc.
} 

print 0+@files, " files converted.\n";
于 2012-08-01T14:50:28.597 回答