I'm experiencing problems with a script converting numbers to bytes.
- If the number is large enough to hit gigabytes or terabytes, the script runs as it should.
- If the number converts to kilobytes, it runs the script, shows number in kilobytes and sends an error message at the same time.
- If I'm in the megabyte interval, it only sends the error message:
line n ((: x > 1048576 : syntax error: invalid arithmetic operator (error token is " "). x represents the number I input.
tb=1099511627776
gb=1073741824
mb=1048576
kb=1024
read number
if (( $number > $tb )); then
echo "$(( number / tb )) terabytes"
elif (( $number > $gb )) && (( $number < $tb )); then
echo "$(( number > gb )) gigabytes"
elif (( $number > $mb )) && (( $number < $gb )); then
echo "$(( number > mb )) megabytes"
elif (( $number > $kb )) && (( $number < $mb )); then
echo "$(( number > mb )) kilobytes"
fi
Line n
is the middle elif
.
)
in line 10.echo "$(( number > gb ))
toecho "$(( number / gb ))
== symbol>
by/
. More would like to testnumfmt --to=iec
command.elif
, eliminate the&&
because it's already true.mb
withkb
in the kilobytes case, and thinking about what happens when the input is exactly equal to one of the constants (you can get rid of about half of the comparisons), or if it is lower than$kb
.t=$(((g=(m=(k=1024*(b=1))*k)*k)*k))
, you can then do the check likefor s in t g m k b; do [ "$((n>=$s))" -ne 0 ] && break; done || ! echo 0 bytes\! && echo "$((n/$s))${s#b}b"