Skip to content

Commit 1d700d5

Browse files
committed
🐞 Use awk instead of bc for byte formatting
The -z bc option is not standard.
1 parent 1f5a3e5 commit 1d700d5

File tree

1 file changed

+3
-7
lines changed

1 file changed

+3
-7
lines changed

lib/fmt.sh

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ declare -ga __BYTES_UNITS=(
3434
)
3535

3636
fmt::__deps() {
37-
sys::ensure_has_commands bc
37+
sys::ensure_has_commands awk
3838
}
3939

4040
fmt::__get_unit_factor() {
@@ -78,7 +78,7 @@ fmt::bytes() {
7878
local max_unit=${2:-EB}
7979

8080
local -i magnitude
81-
magnitude=$(echo "scale=0; l($bytes) / l(1024)" | bc -l)
81+
magnitude=$(awk "BEGIN {print int(log($bytes) / log(1024))}")
8282

8383
local unit=''
8484
if [[ -n "$max_unit" ]]; then
@@ -102,11 +102,7 @@ fmt::bytes_to() {
102102
local value
103103

104104
conversion_factor=$(fmt::__get_unit_factor "$unit")
105-
value=$(echo "scale=2; $bytes / $conversion_factor" | bc -zl)
106-
# Add .00 if the value has no dot
107-
if [[ $value != *"."* ]]; then
108-
value+='.00'
109-
fi
105+
value=$(awk "BEGIN {printf \"%.2f\", $bytes / $conversion_factor}")
110106

111107
echo "$value $unit"
112108
}

0 commit comments

Comments
 (0)