2011-11-07 21 views
0

讀取文件時出現錯誤,下面是腳本。Perl語法錯誤:讀取文件的示例程序

#!/bin/bash 
$file = "SampleLogFile.txt"; #--- line 2 
open(MYINPUTFILE,$file);  #--- line 3 
while(<**MYINPUTFILE**>) { 

# Good practice to store $_ value because 
# subsequent operations may change it. 
my($line) = $_; 

# Good practice to always strip the trailing 
# newline from the line. 
chomp($line); 

# Convert the line to upper case. 
print "$line" if $line = ~ /sent/; 

} 
close (MYINPUTFILE); 

輸出:

PerlTesting_New.ksh [2]:=:找不到
PerlTesting_New.ksh [3]:在3行語法錯誤:`(」意外

不限知道是什麼的問題是什麼?

回答

5

變化

#!/bin/bash 

#!/usr/bin/perl 

否則Perl將不會被解釋你的腳本。更改路徑因此,按您的系統

+2

'!#/ usr/bin/env perl'的解釋器行對於找到可以在PATH中找到的Perl很有用。 – JRFerguson

+0

我是一個新手,儘管我按照 – crackerplace

+0

更改了相同的錯誤你是如何調用腳本的? – choroba

0

嗯,你的第一行:#/斌/慶典

/斌/慶典:這是Bash shell中。

您可能需要改變,以

!在/ usr/bin中/ perl的

+0

它雖然不工作 – crackerplace

+0

什麼是錯誤? – jasonfungsing

+0

與我上面張貼的相同erro – crackerplace

1

好了,不管是誰教你寫的Perl這樣需要遷出九十年代。

#!/usr/bin/perl 

use strict; # ALWAYS 
use warnings; # Also always. 

# When you learn more you can selectively turn off bits of strict and warnings 
# functionality on an as needed basis. 


use IO::File; # A nice OO module for working with files. 

my $file_name = "SampleLogFile.txt"; # note that we have to declare $file now. 

my $input_fh = IO::File->new($file_name, '<'); # Open the file read-only using IO::File. 

# You can avoid assignment through $_ by assigning to a variable, even when you use <$fh> 

while(my $line = $input_fh->getline()) { 

    # chomp($line); # Chomping is usually a good idea. 
        # In this case it does nothing but screw up 
        # your output, so I commented it out. 

    # This does nothing of the sort: 
    # Convert the line to upper case. 
    print "$line" if $line = ~ /sent/; 

} 

你也可以用一個襯墊做到這一點:

perl -pe '$_ = "" unless /sent/;' SampleLogFile.txt 

對單行的詳細信息,請參閱perlrun

+0

嘿非常感謝你的投入......乾杯 – crackerplace