Created
August 6, 2015 13:06
-
-
Save silasdavis/baf92c89179bce4a56d4 to your computer and use it in GitHub Desktop.
Convert integral bytes ot human friendly format. Reads number of bytes from stdin or argument or both using '-' for replacement
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
function human-bytes { | |
numfmt --to=iec-i --suffix=B --padding=7 "$@" | |
} | |
if [ "$#" -eq 0 ] | |
then | |
# Is stdin a terminal? | |
if [ -t 0 ]; then | |
cat << EOF | |
Prints bytes in more human-readable form. | |
Usage: human-bytes bytes [more-bytes] | |
Where bytes should be a number of bytes, or '-' to read from stdin. | |
EOF | |
else | |
while read line | |
do | |
human-bytes $line | |
done | |
fi | |
fi | |
for arg in "$@" | |
do | |
if [ $arg == "-" ] | |
then | |
while read line | |
do | |
human-bytes $line | |
done | |
else | |
human-bytes $arg | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment