🐛 free-gigs: make it more portable
This commit is contained in:
parent
c17ba1bd8c
commit
beba25bd1f
1 changed files with 20 additions and 11 deletions
31
free-gigs
31
free-gigs
|
@ -1,20 +1,29 @@
|
|||
#!/bin/bash
|
||||
#!/bin/sh
|
||||
# script-friendly df
|
||||
|
||||
factor=1024 # default
|
||||
factor=1024 # for the default (GiB); use -H for GB
|
||||
|
||||
OPTSTRING="H"
|
||||
while getopts ${OPTSTRING} opt; do
|
||||
case ${opt} in
|
||||
H)
|
||||
factor=1000
|
||||
;;
|
||||
optstring="H"
|
||||
while getopts "$optstring" opt; do
|
||||
case $opt in
|
||||
h) factor=1024 ;; # default (GiB)
|
||||
H) factor=1000 ;; # decimal (GB)
|
||||
esac
|
||||
done
|
||||
shift $((OPTIND-1))
|
||||
|
||||
target=${1:-.}
|
||||
|
||||
free_bytes=$(findmnt --df -T "$target" -oAVAIL --bytes | sed '1d')
|
||||
free_gigs=$((free_bytes/(factor**3)))
|
||||
echo $free_gigs
|
||||
if free_bytes=$(stat -f --format="%a*%S" "$target" 2>/dev/null); then
|
||||
# GNU stat (Linux)
|
||||
:
|
||||
elif free_bytes=$(stat -f "%a*%S" "$target" 2>/dev/null); then
|
||||
# BSD/macOS stat
|
||||
:
|
||||
else
|
||||
# BusyBox stat fallback
|
||||
free_bytes=$(stat -f "$target" | awk '/Block size:/ {bs=$3} /Available:/ {av=$2} END {print av*bs}')
|
||||
fi
|
||||
|
||||
free_gb=$((free_bytes / factor / factor / factor))
|
||||
echo "$free_gb"
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue