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