diff --git a/files/shared/uptime.sh b/files/shared/uptime.sh index 884d79c..2b5a842 100755 --- a/files/shared/uptime.sh +++ b/files/shared/uptime.sh @@ -1,21 +1,32 @@ #!/usr/bin/env bash -uptime_seconds=$(awk '{print $1}' /proc/uptime) -echo -n $(awk -v UP="$uptime_seconds" ' -BEGIN { - days = int(UP / 86400); - hours = int((UP % 86400) / 3600); - minutes = int((UP % 3600) / 60); - seconds = int(UP % 60); - if (days > 0) { - if (days == 1) { - printf "1 day, %d hours, %d minutes\n", hours, minutes; - } else { - printf "%d days, %d hours, %d minutes\n", days, hours, minutes; - } - } else if (hours > 0) { - printf "%d hours, %d minutes\n", hours, minutes; - } else { - printf "%d minutes\n", minutes; - } -}') +uptime_seconds=$(cat /proc/uptime | sed 's/^\([0-9]\+\)\..*/\1/') + +# Format the uptime +days=$(( uptime_seconds / 86400 )) +hours=$(( (uptime_seconds % 86400) / 3600 )) +minutes=$(( (uptime_seconds % 3600) / 60 )) + +# Helper function for pluralization +pluralize() { + local value=$1 + local singular=$2 + local plural=$3 + if [ "$value" -eq 1 ]; then + echo "$value $singular" + else + echo "$value $plural" + fi +} + +# Construct the uptime string +if [ "$days" -gt 0 ]; then + uptime="$(pluralize $days day days), $(pluralize $hours hour hours), $(pluralize $minutes minute minutes)" +elif [ "$hours" -gt 0 ]; then + uptime="$(pluralize $hours hour hours), $(pluralize $minutes minute minutes)" +else + uptime="$(pluralize $minutes minute minutes)" +fi + +echo "$uptime" +