gpt4 book ai didi

linux - Perl,对存储在哈希中的用户使用 linux 命令

转载 作者:太空宇宙 更新时间:2023-11-04 09:26:55 25 4
gpt4 key购买 nike

代码如下:

#!/usr/bin/perl

use warnings;
use strict;
use utf8;


my @temparray;
my $count = 0;
my @lastarray;
my $lastbash;


#Opens the file /etc/shadow and puts the users with an uid over 1000 but less that 65000 into an array.
open( my $passwd, "<", "/etc/passwd") or die "/etc/passwd failed to open.\n";

while (my $lines = <$passwd>) {
my @splitarray = split(/\:/, $lines );
if( $splitarray[2] >= 1000 && $splitarray[2] < 65000) {

$temparray[$count] =$splitarray[0];
print "$temparray[$count]\n";
$count++;
}
}
close $passwd;

foreach (@temparray) {
$lastbash = qx(last $temparray);
print "$lastbash\n";
}

我想要做的是对存储在@temparray 中的所有用户使用内置的 linux 命令“last”。我希望输出是这样的:

用户 1:10

用户 2:22

其中 22 和 10 是他们登录的次数。我怎样才能做到这一点?我尝试了几种不同的方法,但总是以错误告终。

最佳答案

以下应按要求执行任务:

#!/usr/bin/perl

use warnings;
use strict;
use utf8;


my @temparray;
my $count = 0;
my @lastarray;
my $lastbash;


#Opens the file /etc/shadow and puts the users with an uid over 1000 but less that 65000 into an array.
open( my $passwd, "<", "/etc/passwd") or die "/etc/passwd failed to open.\n";

while (my $lines = <$passwd>) {
my @splitarray = split(/\:/, $lines );
if( $splitarray[2] >= 1000 && $splitarray[2] < 65000) {

$temparray[$count] =$splitarray[0];
print "$temparray[$count]\n";
$count++;
}
}
close $passwd;

foreach (@temparray) {
my @lastbash = qx(last $_); #<----Note the lines read in go to the $_ variable. Note use of my. You also read the text into array.
print $_.":".@lastbash."\n"; #<----Note the formatting. Reading @lastbash returns the number of elements.
}

关于linux - Perl,对存储在哈希中的用户使用 linux 命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35416437/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com