Perl-使用打印函数时获得意外结果

Perl-使用打印函数时获得意外结果,perl,printing,Perl,Printing,您好,我正在尝试创建一个包含以下内容的文件 $outfile = `hostname`; $txt = "\nHealth check results - "; $txt .= `hostname`; $txt .= "\n==========================================="; print "$txt"; # Create output file to store the HC results. open $fh, '>', $outfile or

您好,我正在尝试创建一个包含以下内容的文件

$outfile = `hostname`;
$txt = "\nHealth check results - ";
$txt .= `hostname`;
$txt .= "\n===========================================";
print "$txt";

# Create output file to store the HC results.

open $fh, '>', $outfile or die "Can't open $outfile \n";
print $fh $txt;
close ($fh)
输出如下所示

运行状况检查结果-XXHOSTNAMEXX

==========================================================XXHOSTNAMEXX

为什么主机名会被打印两次,我不需要在最后第二次打印主机名


此外,我还尝试创建文件名作为主机名,正如您在代码中看到的那样,它将文件名创建为主机名,但是我看到文件名后面附加了一个换行符,我在目录中列出文件时注意到了这一点(使用ls-ltr)(即,文件名itlself显示为两行-第一行是主机名,并在文件名后附加一个空换行符)

您需要使用
chomp()
函数删除获取
主机名entermark

$outfile = `hostname`;

chomp $outfile;  #----> Need to chomp (Remove last entermark)

$txt = "\nHealth check results - ";

$txt .= $outfile; #----> Already you got the hostname hence just store $outfile 

$txt .= "\n===========================================";
print "$txt";

# Create output file to store the HC results.

open $fh, '>', $outfile or die "Can't write $outfile \n";
print $fh $txt;
close ($fh)

您需要使用
chomp()
函数删除获取
hostname entermark

$outfile = `hostname`;

chomp $outfile;  #----> Need to chomp (Remove last entermark)

$txt = "\nHealth check results - ";

$txt .= $outfile; #----> Already you got the hostname hence just store $outfile 

$txt .= "\n===========================================";
print "$txt";

# Create output file to store the HC results.

open $fh, '>', $outfile or die "Can't write $outfile \n";
print $fh $txt;
close ($fh)
线路

$outfile = `hostname`;
将运行
hostname
命令并返回写入STDOUT的所有数据,包括终止EOL。这意味着您将得到一个包含终止EOL字符的文件名。文件系统对该文件的处理取决于操作系统,但在Linux上,您将得到一个包含EOL字符的文件

您可以使用
chomp

$outfile = `hostname`;
chomp $outfile;
更新后的脚本如下所示

$outfile = `hostname`;
chomp $outfile;

$txt = "\nHealth check results - ";
$txt .= $outfile;
$txt .= "\n===========================================\n";
print "$txt";

# Create output file to store the HC results.

open $fh, '>', $outfile or die "Can't open $outfile \n";
print $fh $txt;
close ($fh) ;
当我运行它时,我得到了这个

Health check results - XXHOSTNAMEXX
===========================================
线路

$outfile = `hostname`;
将运行
hostname
命令并返回写入STDOUT的所有数据,包括终止EOL。这意味着您将得到一个包含终止EOL字符的文件名。文件系统对该文件的处理取决于操作系统,但在Linux上,您将得到一个包含EOL字符的文件

您可以使用
chomp

$outfile = `hostname`;
chomp $outfile;
更新后的脚本如下所示

$outfile = `hostname`;
chomp $outfile;

$txt = "\nHealth check results - ";
$txt .= $outfile;
$txt .= "\n===========================================\n";
print "$txt";

# Create output file to store the HC results.

open $fh, '>', $outfile or die "Can't open $outfile \n";
print $fh $txt;
close ($fh) ;
当我运行它时,我得到了这个

Health check results - XXHOSTNAMEXX
===========================================

“为什么要打印两次主机名…”很难理解为什么会有第二个主机名..请尝试在
print
命令中插入单引号以调试:
print“$txt”
现在
print
语句的输出是什么,输出中的单引号在哪里?“为什么要打印两次主机名…”很难理解为什么会有第二个主机名..尝试在
print
命令中插入单引号以调试:
print“'$txt”“
print语句现在的输出是什么,输出中的单引号在哪里?非常感谢!非常好,我注意到的一点是,在下面一行的末尾,一个新行(\n)是必需的,另一方面,我看到主机名在第二行的末尾打印,
$txt=”“\n=======================================================\n”
非常感谢!非常感谢!我注意到,在下面的一行末尾,一个新行(\n)是必需的,另一方面,我看到主机名第二次打印在行的末尾
$txt.=”\n=======================================================================================================================================================================================================================================================================================================================================================================================