Rewrite uptime

This commit is contained in:
Dylan Araps 2016-10-21 10:00:09 +11:00
parent 5274d2d54a
commit fb7b3c146e
1 changed files with 27 additions and 46 deletions

View File

@ -269,65 +269,46 @@ getkernel() {
getuptime() {
case "$os" in
"Linux" | "Windows")
case "$distro" in
*"Puppy"* | "Quirky Werewolf"* | "Alpine Linux"* | "OpenWRT"* | "Windows"*)
uptime="$(uptime | awk -F ':[0-9]{2}+ |(, ){1}+' '{printf $2}')"
"Linux" | "Windows" | "Mac OS X" | "iPhone OS" | "BSD")
# Get uptime in seconds
case "$os" in
"Linux" | "Windows")
seconds="$(< /proc/uptime)"
seconds="${seconds/.*}"
;;
"openSUSE"*)
uptime="$(uptime | awk -F ':[0-9]{2}+[a-z][a-z] |(, ){1}+' '{printf $2}')"
;;
"Mac OS X" | "iPhone OS" | "BSD")
boot="$(sysctl -n kern.boottime)"
boot="${boot/'{ sec = '}"
boot="${boot/,*}"
*)
uptime="$(uptime -p)"
[ "$uptime" == "up " ] && uptime="up $(awk -F'.' '{print $1}' /proc/uptime) seconds"
# Get current date in seconds
now="$(date +%s)"
seconds="$((now - boot))"
;;
esac
;;
"Mac OS X" | "iPhone OS" | "BSD")
# Get boot time in seconds
boot="$(sysctl -n kern.boottime)"
boot="${boot/'{ sec = '}"
boot="${boot/,*}"
days="$((seconds / 86400)) days"
hours="$((seconds / 3600)) hours"
minutes="$((seconds % 3600 / 60)) minutes"
# Get current date in seconds
now="$(date +%s)"
uptime="$((now - boot))"
# Convert uptime to days/hours/mins
minutes="$((uptime / 60%60))"
hours="$((uptime / 3600%24))"
days="$((uptime / 86400))"
case "$minutes" in
1) minutes="1 minute" ;;
0) unset minutes ;;
*) minutes="$minutes minutes" ;;
case "$days" in
"0 days") unset days ;;
"1 days") days="${days/s}" ;;
esac
case "$hours" in
1) hours="1 hour" ;;
0) unset hours ;;
*) hours="$hours hours" ;;
"0 hours") unset hours ;;
"1 hours") hours="${hours/s}" ;;
esac
case "$days" in
1) days="1 day" ;;
0) unset days ;;
*) days="$days days" ;;
case "$minutes" in
"0 minutes") unset minutes ;;
"1 minutes") minutes="${minutes/s}" ;;
esac
[ "$hours" ] && \
[ "$minutes" ] && \
hours+=","
[ "$days" ] && \
[ "$hours" ] && \
days+=","
uptime="up $days $hours $minutes"
uptime="up ${days:+$days,} ${hours:+$hours,} ${minutes}"
uptime="${uptime%', '}"
;;
"Solaris")
@ -353,7 +334,7 @@ getuptime() {
uptime="${uptime/ minutes/m}"
uptime="${uptime/ minute/m}"
uptime="${uptime/ seconds/s}"
uptime="${uptime/,}"
uptime="${uptime//,}"
;;
esac
}