diff options
author | P. J. McDermott <pj@pehjota.net> | 2018-05-04 01:38:16 (EDT) |
---|---|---|
committer | P. J. McDermott <pj@pehjota.net> | 2018-05-04 01:38:16 (EDT) |
commit | 091612802226a9aa3fb8538a9eef8e3bcfe74669 (patch) | |
tree | 9d7c38522c98dbda970a654273a037c898bc9b2b | |
parent | 05954c3f6270fa1973376fc6914608d5ef920b95 (diff) | |
download | fluxbox-091612802226a9aa3fb8538a9eef8e3bcfe74669.zip fluxbox-091612802226a9aa3fb8538a9eef8e3bcfe74669.tar.gz fluxbox-091612802226a9aa3fb8538a9eef8e3bcfe74669.tar.bz2 |
bin/df-uptime: New script
-rwxr-xr-x | bin/df-uptime | 110 |
1 files changed, 110 insertions, 0 deletions
diff --git a/bin/df-uptime b/bin/df-uptime new file mode 100755 index 0000000..64ac8b4 --- /dev/null +++ b/bin/df-uptime @@ -0,0 +1,110 @@ +#!/bin/sh + +set -eu + +indent='' +le="$(printf '\n.')"; le="${le%.}" + +err() +{ + local fmt="${1}" + shift 1 + + printf "Error: ${fmt}\n" "${@}" 1>&2 + return 0 +} + +gen_indent() +{ + local l=${1} + shift 1 + local i= + + i=0 + while [ ${i} -lt ${l} ]; do + indent="${indent} " + i=$((${i} + 1)) + done + + return 0 +} + +do_df() +{ + local sed_bre= + + sed_bre='^..* *..* *..* *..* *\([0-9][0-9]*%\) \(..*\)$' + df -h -x devtmpfs -x tmpfs | \ + sed "1d; s/${sed_bre}/${indent}\\1 \\2/;" | tr '\n' "${le}" + + return 0 +} + +do_uptime() +{ + local up= + local idle= + local min= + local hr= + local day= + + read -r up idle 0</proc/uptime + + up="${up%.*}" + min=$((${up} / 60)) + hr=$((${min} / 60)) + day=$((${hr} / 24)) + + min=$((${min} % 60)) + hr=$((${hr} % 24)) + + printf '%sUp %d d %2d:%02d%c' "${indent}" ${day} ${hr} ${min} "${le}" + + return 0 +} + +usage() +{ + printf 'Usage: %s [<options>]\n' "${0}" + printf '\n' + printf 'Options:\n' + printf ' -i <num> Indent lines with <num> spaces\n' + printf ' -r Print carriage returns instead of line feeds\n' + + return 0 +} + +main() +{ + local opt= + + while getopts 'i:r' opt "${@}"; do + case "${opt}" in + 'i') + case "${OPTARG}" in *[!0-9]* | '') + usage 1>&2 + return 1 + esac + gen_indent ${OPTARG} + ;; + 'r') le="$(printf '\r.')"; le="${le%.}";; + '?') + usage 1>&2 + return 1 + ;; + esac + done + shift $((${OPTIND} - 1)) + + if [ ${#} -ne 0 ]; then + usage 1>&2 + return 1 + fi + + do_df + do_uptime + + return 0 +} + +main "${@}" |