简体   繁体   中英

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 -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

This is the perl code

#!/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.

You could also put it at the end of your 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;'.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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