reboot system boot 3.10.0-327.el7.x Wed Oct 26 15:12 - 22:43 (07:30)
root :0 :0 Wed Oct 5 05:01 - 05:31 (00:29)
(unknown :0 :0 Wed Oct 5 05:01 - 05:01 (00:00)
reboot system boot 3.10.0-327.el7.x Tue Oct 4 23:01 - 05:31 (06:29)
root :0 :0 Wed Oct 5 04:56 - 04:58 (00:01)
(unknown :0 :0 Wed Oct 5 04:56 - 04:56 (00:00)
reboot system boot 3.10.0-327.el7.x Tue Oct 4 22:55 - 04:58 (06:02)
任何人都可以告诉我如何在 Linux 中编写一个命令来计算每个用户的登录次数并计算所花费的总时间。将其投影到显示此内容的屏幕上。
User root logged in a total of ?? times with a total of mins/hours logged
User unknown logged in a total of ?? times with a total of mins/hours logged.
答案1
reboot system boot 3.10.0-327.el7.x Wed Oct 26 15:12 - 22:43 (07:30)
root :0 :0 Wed Oct 5 05:01 - 05:31 (00:29)
(unknown :0 :0 Wed Oct 5 05:01 - 05:01 (00:00)
reboot system boot 3.10.0-327.el7.x Tue Oct 4 23:01 - 05:31 (06:29)
root :0 :0 Wed Oct 5 04:56 - 04:58 (00:01)
(unknown :0 :0 Wed Oct 5 04:56 - 04:56 (00:00)
reboot system boot 3.10.0-327.el7.x Tue Oct 4 22:55 - 04:58 (06:02)
考虑到上述详细信息位于文件中:
for i in `awk '{print $1}' test_login |sort|uniq`;
do
count=`grep -c $i test_login`
sum=0;
for j in `grep $i test_login|awk '{print $NF}'|cut -c 2-6`;
do
var=`echo $j | awk -F: '{print ($1 * 60) + $2}'`;
sum=$((sum+var));
done
((hour=$sum/60))
((min=$sum-$hour*60))
echo "User $i logged in a total of $count times with a total of $hour:$min logged"
done
test_login :包含所有用户日志的文件。