I know I can use finger command to display all the current users logged into the system. However, how can I list the users who are currently logged into the system more than once?
Asked
Active
Viewed 259 times
0
-
StackOverflow is dedicated to helping solve programming code problems. Your Q **may be** more appropriate for [su] , but read their help section regarding on-topic questions . AND please read [Help On-topic](https://stackoverflow.com/Help/On-topic) and [Help How-to-ask](https://stackoverflow.com/Help/How-to-ask) before posting more Qs here. Good luck. – shellter Jul 26 '20 at 02:00
3 Answers
0
Try
last | cut -d ' ' -f1 | sort | uniq -c | grep -Ev 'reboot|wtmp'
last
<-- last searches back through the /var/log/wtmp file (or the file designated by the -f option) and displays a list of all users logged
in (and out) since that file was created.
cut -d ' ' -f1
<-- cut user name
sort
-- Sort data
uniq -c
<-- Get count of user
grep -Ev
'reboot|wtmp' <-- remove non required records

Digvijay S
- 2,665
- 1
- 9
- 21
0
A crude bash script:
#!/bin/bash
arr=($(users))
tmp=${arr[0]}
cnt=1
for ((i = 1; i < ${#arr[@]}; ++i)); do
if [ "$tmp" == "${arr[$i]}" ]; then
cnt=$((cnt+1))
else
if [ "$cnt" -gt "1" ]; then
echo "${arr[$i]} is logged in more than once."
fi
tmp=${arr[$i]}
cnt=1
fi
done
if [ "$cnt" -gt "1" ]; then
echo "${arr[$i]} is logged in more than once."
fi
Loop over an array created out of the output of users
command.
Check if tmp
is equal to the element coming after it:
if yes: echo,
else: change tmp
to next element
NOTE:
arr=($(users))
should be avoided generally, but for this particular problem is doesn't cause issues.
SEE:

Jacek Kuszmar
- 80
- 1
- 8