- #!/bin/bash
- #This script is used to general a statistics report for user login
- #ken.zhang
- while :
- do
- read -p "please input username:" username
- count=`cat /etc/passwd|cut -d: -f1|grep -c "$username\>"`
- #count=`grep -c $username /etc/passwd` # 非精确匹配,输入roo 等也会计算在内,不符合要求。
- if [ $count -eq 0 ];then
- echo "Not a valid user!!!"
- continue;
- else
- logintime=`last|grep -c "$username"`
- if [ $logintime -eq 0 ];then
- echo "User never login via console!!!"
- continue;
- fi
- fi
- hour=`last|grep -E 'pts|tty'|grep "$username"|grep -v 'still'|awk -F '(' '{print $2}'|awk -F ')' '{print $1}'|awk -F ':' '{sum+=$1};END{print sum}'`
- min=`last|grep -E 'pts|tty'|grep "$username"|grep -v 'still'|awk -F '(' '{print $2}'|awk -F ')' '{print $1}'|awk -F ':' '{sum+=$2}END{print sum}'`
- let "fhour=$hour+$min/60"
- let "fmin=$min%60"
- cat << EOL
- ########## User Login Report #########
- --- User Name --- Total Login Time ---
- $username $fhour hour $fmin min
- EOL
- done