简体   繁体   English

将perl结果输出到文件中

[英]Output perl results into file

I have a perl script that works fine when I run it using perl filename, however when I use the command 当我使用perl文件名运行它时,我有一个perl脚本可以正常工作,但是当我使用命令时

perl -w logint > logintime.html

I get this error 我得到这个错误

Use of uninitialized value $days in multiplication (*) at logint line 5, <LAST> line 3.

It repeats this from line 3-47 它从第3-47行重复

This is the perl code 这是perl代码

#!/usr/bin/perl
open LAST, "last |";
while (<LAST>) {
    if (($name,$days,$hours,$mins) = /^(\w+).+\((?:(\d+)\+)?(\d+):(\d+)/) {
        $TIMES{$name} += 1440 * $days + 60 * $hours + $mins;
    }
}
foreach (sort keys %TIMES) {
    print "$_ $TIMES{$_}\n";
}

This is how I'm attempting to output it. 这就是我试图输出的方式。

#!/bin/bash
echo $HDR > ~/public_html/logintime.html
perl -w logint > logintime.html
echo $FTR >> ~/public_html/logintime.html

This is just a warning, it's not an error. 这只是警告,不是错误。 You're seeing it when you run that command because '-w' is the warnings pragma. 您在运行该命令时会看到它,因为“ -w”是警告提示。

You could also put it at the end of your shebang 您也可以将其放在shebang的结尾

    #!/usr/bin/perl -w

Or 'use warnings;'. 或“使用警告;”。 Anyway, the warning is just saying it doesn't have a value. 无论如何,警告只是说它没有价值。 It looks like you're reading the last log to see who last logged in, the output can be different depending on what OS you're on. 您似乎正在阅读最新的日志以查看谁最近一次登录,所以输出可能会有所不同,具体取决于您所使用的操作系统。 I would confirm it's working as expected and getting the correct values. 我会确认它能按预期工作并获得正确的值。

It's also best practice to use 'use strict;'. 使用“严格使用”也是一种最佳做法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM