-
Notifications
You must be signed in to change notification settings - Fork 0
/
space
executable file
·39 lines (34 loc) · 974 Bytes
/
space
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
#!/usr/bin/env bash
declare available=0
declare total=0
declare percent=0
has() {
command -v "$1" &> /dev/null
}
unitConversion() {
awk 'function unitConversion(num) {
split("K M G T", unit);
pointer=1;
while(num>=1025 && pointer < 4) {
num = num/1024; pointer++;
}
return sprintf("%0.1f%s", num, unit[pointer]);
}
{ for(i = 1; i <= NF; i++) printf "%s\n", unitConversion($i); }' <<< "$@"
}
getSpaceAvailable() {
local used _
if has quota && quota &> /dev/null && [[ -n $(quota) ]] && [[ $(quota) != *none ]]; then
read -r _ used total _ < <(quota -w | tail -n1)
else
read -r used total < <(df --total --output=used,size | tail -n1)
fi
available=$(( total - used ))
percent=$(( available * 100 / total ))
}
getSpaceAvailable
if [[ $1 == -s ]]; then
printf "%s/%s\n" $(unitConversion "$available" "$total")
else
printf "You have %s remaining of %s [%d%%]\n" $(unitConversion "$available" "$total") "$percent"
fi