我想将passwd文件中的每个用户与影子文件中的条目进行比较,如果影子文件中的条目与999999匹配,则打印出passwd文件的整行.Perl中最简单的方法是什么这个?或者我想我可以从一个文件中取出值并在另一个文件中匹配?这样做的最佳方式是什么?
答案 0 :(得分:3)
awk -F":" 'FNR==NR&&$5=="99999"{user[$1];next}($1 in user)' /etc/shadow /etc/passwd
如果要排除包含“!!”的行
,请将FNR==NR&&$5=="99999"
更改为FNR==NR&&$5=="99999"&&$2!="!!"
答案 1 :(得分:2)
#! /usr/bin/perl
use warnings;
use strict;
sub read_passwd {
open my $fh, "<", "/etc/passwd" or die "$0: open: $!";
my %passwd;
while (<$fh>) {
next unless /^([^:]+)/;
$passwd{$1} = $_;
}
\%passwd;
}
my $passwd = read_passwd;
open my $fh, "<", "/etc/shadow" or die "$0: open: $!";
while (<$fh>) {
my($user,$maxage) = (split /:/)[0,4];
next unless $maxage eq 99999;
if ($passwd->{$user}) {
print $passwd->{$user};
}
else {
warn "$0: no passwd entry for '$user'";
}
}
答案 2 :(得分:0)
sudo perl -F: -lane '(1..eof)?($_{$F[0]}=$_):/999999/&&($_=$_{$F[0]})&&print' /etc/passwd /etc/shadow
答案 3 :(得分:0)
您可以使用AnyData::Format::Password:
#!/usr/bin/perl
use strict; use warnings;
use AnyData;
my $passwd = adTie(Passwd => 'passwd' );
my $shadow = adTie(Passwd => 'shadow' );
for my $user (keys %$shadow) {
if ( $user->{fullname} and $user->{fullname} eq '999999' ) {
print $passwd->{$user->{username}}{fullname}, "\n";
}
}
输出:
... Privilege-separated SSH RPC Service User Anonymous NFS User HAL daemon
或者:
for my $user (keys %$shadow) {
if ( $user->{fullname} and $user->{fullname} eq '999999' ) {
my @values = map { defined $_ ? $_ : '' }
@{ $passwd->{$user->{username}} }{@fields};
print join(':', @values), "\n";
}
}