#!/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
Asked
Active
Viewed 179 times
-2

ctrl-alt-delor
- 27,993

Student
- 1
2 Answers
0
A simple perl script ...
#!/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;

ingopingo
- 807
-
1Hello ingopingo. Could you please explain to the OP what this does and how this works, so that they (and others) can learn from you. (The question itself isn't clear, so it would probably be useful for you to clarify what you've understood from it, so that that OP can determine whether or not your answer is useful.) – Chris Davies Jun 15 '19 at 14:18
-1
This is my assumption on what you want to do.
#!/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
Example output will be assuming AAA (with name AAA AAA on passwd) has 3, BBB(with name BBB BBB on passwd) has and CC (with name CCC CCC on passwd) has 1 entries on who
. the output must be:
Student AAA AAA has number of 3 logins.
Student BBB BBB has number of 2 logins.
Student CCC CCC has number of 1 logins.

ebvjr
- 174
-
1
-
Sorry, it says "Usage: grep [OPTION]... PATTERN [FILE]... Try 'grep --help' for more information.
I forgot to say that im using PUTTY, maybe its a bit different.
– Student Jun 15 '19 at 14:26 -
Now that you have quoted
$unique
it will no longer expand in the way you initially intended. You will have to use an array instead of a variable. – jesse_b Jun 15 '19 at 14:36 -
-
1
who
? What is your script returning? What would you like it to return? – jesse_b Jun 15 '19 at 13:22grep "10\.1\.109"
. Why are you doinggrep -v berianho
? – jesse_b Jun 15 '19 at 13:49echo -n
will do it. If this does not do it for you, then you need to edit the question. – ctrl-alt-delor Jun 15 '19 at 15:28