2012-08-01 70 views
1

排名初學者請溫柔... 我在Perl編寫一個程序,查找所有特定文件類型,通話和其他程序調用newstack的轉換的文件類型。的Perl:從shell命令的作品,但沒有系統()這裏

當我從shell中運行newstack oldfileame newfilename它工作正常。如果我寫一個shell腳本,做同樣的事情,在文件中的一個,在正常工作時間運行newstack

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

:我的程序運行system("newstack oldfileame newfilename") newstack返回錯誤時

。有什麼我在這裏失蹤爲什麼它在perl程序的上下文中運行失敗?

新版本是從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"; 

我行8後添加chop $mrc,它解決了這一問題

+2

從外殼的錯誤消息指示)有一個新行或「;」在'system'通話newstack'和b)你不顯示實際的Perl代碼之後'... – pavel 2012-08-01 14:32:41

+0

謝謝!我所缺少的是爲'chop'一個變量 - 修改原來的職位,顯示代碼,以幫助他人。 – attamatti 2012-08-01 14:49:53

+1

它是更好(更易讀),如果把你的代碼中的問題......而dom't使用'chop';用chomp代替... – pavel 2012-08-01 14:50:57

回答

2

您發佈的代碼,你執行的代碼不同。在你執行的代碼,有後newstack

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

一個換行符刪除使用chomp($x)或使用$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 [email protected], " files converted.\n"; 
+0

謝謝!修復了問題 – attamatti 2012-08-01 14:55:31

+0

已更新,以解決您的更新問題。 – ikegami 2012-08-01 22:20:45