-1

好的,这让我发疯了!

查看了许多示例并阅读了 perl 中的 if 语句,一切对我来说都是正确的,所以其他人可以发现错误吗?

#Start of script
#!/usr/bin/perl -w

##########################
#### Define Variables ####
##########################
echo $PWD;
mainDirectory=$ENV{HOME}"/test/";
file='report.txt';
backupDirectory=$ENV{HOME}"/test/backup";
number_to_try=0;

##################################
#### Check if the file exists ####
################################## 
filename=$mainDirectory$file;
echo $filename;

if (-e $filename) {
    print "File Exists!"
}

我得到的错误信息是:

./perl.pl: line 18: syntax error near unexpected token `{'
./perl.pl: line 18: `if (-e $filename) {'

有人有想法么??

4

3 回答 3

6

"if" 上面的所有行都不是有效的 Perl;我相信你想做:

#!/usr/bin/perl
use strict;
use warnings;

##########################
#### Define Variables ####
##########################

my $mainDirectory = "$ENV{HOME}/test";
my $file = 'report.txt';
my $backupDirectory = "$ENV{HOME}/test/backup";
my $number_to_try = 0;

##################################
#### Check if the file exists ####
##################################

my $filename = "$mainDirectory/$file";
print "$filename\n";

if (-e $filename) {
        print "File Exists!\n";
}
于 2013-08-23T20:47:59.930 回答
2

您可能想要阅读更多关于 Perl 基础的教程,尤其是语法。

标量变量应以$.

连接字符串时,请使用.运算符

是什么$PWD您可以使用该模块获取当前目录。Cwd

是什么echo你的意思是print

于 2013-08-23T20:50:05.790 回答
1

以最小的变化重写:

#!/usr/bin/perl -w

##########################
#### Define Variables ####
##########################

#echo $PWD;
$mainDirectory=$ENV{HOME}."/test/";
$file='report.txt';
$backupDirectory=$ENV{HOME}."/test/backup";
$number_to_try=0;

##################################
#### Check if the file exists ####
################################## 
$filename=$mainDirectory.$file;
#echo $filename;

if (-e $filename) {
    print "File Exists!"
}

强烈建议使用use strict;虽然use warnings;

于 2013-08-23T20:45:53.323 回答