2 Star 4 Fork 2

晴云孤魂/shell-scripts

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
last-format.sh 1.73 KB
一键复制 编辑 原始数据 按行查看 历史
#!/usr/bin/env bash
#last格式化时间输出
last-format() {
#last命令格式化完整输出,转换并显示为国人适应的日期时间格式(yyyy-mm-dd HH:ii:ss)
#注意此函数Cygwin下可能不可用(last没有-F选项),请在常规Linux系统下运行;
# See Also:https://www.baeldung.com/linux/last-command
# See Also2:https://www.howtogeek.com/416023/how-to-use-the-last-command-on-linux/
# See Also3:https://serverfault.com/questions/375091/getting-login-year-data-with-the-last-command-on-linux
last -F &>/dev/null
local retCode=$?
[ $retCode -ne 0 ] && echo -e "当前系统 last 命令不支持 -F 选项,请在Linux系统终端运行以下命令:"
if [ $retCode -ne 0 ] || [[ "${1,,}" == "--show" || "${1,,}" == "--linux" ]];then #当前系统 last 命令不支持 -F 选项
cat<<'EOF'
last -F|awk '{if(match($1,/reboot/) || match($0,/still logged/)){\
/*print $0;*/
next;
}else{\
begintime=sprintf("%s %s %s %s %s\n",$4,$5,$6,$7,$8);"date +\"%F %T\" -d\""begintime"\""|getline newbegintime;\
endtime=sprintf("%s %s %s %s %s\n",$10,$11,$12,$13,$14);"date +\"%F %T\" -d\""endtime"\""|getline newendtime;\
printf "%s %s %s %s %s %s %s\n",$1,$2,$3,newbegintime,$9,newendtime,$15
}\
}'|column -s ' ' -t
EOF
else
last -F|awk '{if(match($1,/reboot/) || match($0,/still logged/)){\
/*print $0;*/
next;
}else{\
begintime=sprintf("%s %s %s %s %s\n",$4,$5,$6,$7,$8);"date +\"%F %T\" -d\""begintime"\""|getline newbegintime;\
endtime=sprintf("%s %s %s %s %s\n",$10,$11,$12,$13,$14);"date +\"%F %T\" -d\""endtime"\""|getline newendtime;\
printf "%s %s %s %s %s %s %s\n",$1,$2,$3,newbegintime,$9,newendtime,$15
}\
}'|column -s ' ' -t
fi
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Shell
1
https://gitee.com/hexiyou/shell-scripts.git
git@gitee.com:hexiyou/shell-scripts.git
hexiyou
shell-scripts
shell-scripts
master

搜索帮助