Advertisement
tivasyk

the long (and rignt) way to get uptime with bash

Jul 18th, 2019
322
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 0.65 KB | None | 0 0
  1. #!/usr/bin/env bash
  2. # tivasyk <tivasyk@gmail.com>
  3.  
  4. # Alternative (the right) way to get uptime
  5. DUMP_STRING=$(cat /proc/uptime)
  6. printf "Raw data: %s\n" "${DUMP_STRING}"
  7.   TOTAL_SECS=${DUMP_STRING%%.*}
  8. printf "Seconds:  %s\n" "${TOTAL_SECS}"
  9.  
  10. DAYS=$(( TOTAL_SECS / 86400 ))
  11. HOURS=$(( (TOTAL_SECS - DAYS*86400) / 3600 ))
  12. MINUTES=$(( (TOTAL_SECS - DAYS*86400 - HOURS*3600) / 60 ))
  13. SECONDS=$(( TOTAL_SECS - DAYS*86400 - HOURS*3600 - MINUTES*60 ))
  14.  
  15. HOURS="0${HOURS}"; HOURS="${HOURS:(-2)}"
  16. MINUTES="0${MINUTES}"; MINUTES="${MINUTES:(-2)}"
  17. SECONDS="0${SECONDS}"; SECONDS="${SECONDS:(-2)}"
  18.  
  19. printf "Uptime: ${DAYS} days, ${HOURS}:${MINUTES}:${SECONDS}\n"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement