summaryrefslogtreecommitdiffstats
path: root/bin/df-uptime
blob: 64ac8b47b112103157416d474ac6832bacd6ece4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
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 "${@}"