#!/bin/bash
who |grep "10\.1\.109" | grep -v berianho | cut -f1 -d " " | sort -n|
while read user
do
grep -a ^$user: /etc/passwd | cut -f5 -d:
done
答案1
一个简单的 Perl 脚本...
#!/usr/bin/perl
my %hash;
open FH, 'who |' or die;
while ( <FH> ) {
$hash{$1}++ if /^(\S+).*(10\.\d+\.\d+\.\d+)/;
}
close FH;
while ( ($k,$v) = each %hash ) {
printf "%3d %s\n", $v, $k;
}
exit;
答案2
这是我对你想做的事情的假设。
#!/bin/bash
list="$(who |grep "10\.1\.109" | grep -v berianho | cut -f1 -d " ")"
unique="$(echo ${list} | tr ' ' '\n' | sort | uniq)"
for student in $unique
do
echo "Student $(grep -a ^${student}: /etc/passwd | cut -f5 -d ":" ) has number of $(echo "$list" | tr ' ' '\n' | grep ${student} | wc -l) logins."
done
示例输出假设 AAA(passwd 上的名称为 AAA AAA)有 3 个,BBB(passwd 上的名称为 BBB BBB)有,CC(passwd 上的名称为 CCC CCC)有 1 个条目who
。输出必须是:
Student AAA AAA has number of 3 logins.
Student BBB BBB has number of 2 logins.
Student CCC CCC has number of 1 logins.