2011-11-07 23 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