index : pacman | |
Archlinux32 fork of pacman | gitolite user |
summaryrefslogtreecommitdiff |
author | Dave Reisner <dreisner@archlinux.org> | 2012-05-06 18:27:43 -0400 |
---|---|---|
committer | Dan McGee <dan@archlinux.org> | 2012-06-25 23:39:34 -0500 |
commit | a98fce08968febe7395b30f15f070f400da089ed (patch) | |
tree | 419123b22846a05a0eda74db324b6d3741208539 | |
parent | ab8431c92953c4562af214e5170f8ef321037108 (diff) |
-rw-r--r-- | scripts/Makefile.am | 3 | ||||
-rw-r--r-- | scripts/library/README | 4 | ||||
-rw-r--r-- | scripts/library/size_to_human.sh | 20 |
diff --git a/scripts/Makefile.am b/scripts/Makefile.am index 75699b2e..29c81aa5 100644 --- a/scripts/Makefile.am +++ b/scripts/Makefile.am @@ -28,7 +28,8 @@ EXTRA_DIST = \ LIBRARY = \ library/output_format.sh \ library/parseopts.sh \ - library/human_to_size.sh + library/human_to_size.sh \ + library/size_to_human.sh # Files that should be removed, but which Automake does not know. MOSTLYCLEANFILES = $(bin_SCRIPTS) diff --git a/scripts/library/README b/scripts/library/README index 44748ee2..0fa0f847 100644 --- a/scripts/library/README +++ b/scripts/library/README @@ -35,3 +35,7 @@ successful, the converted byte value is written to stdout and the function returns 0. If an error occurs, nothing in written and the function returns 1. Results may be inaccurate when using a broken implementation of awk, such as mawk or busybox awk. + +size_to_human.sh: +The reverse of human_to_size, this function takes an integer byte size and +prints its in human readable format, with SI prefixes (e.g. MiB, TiB). diff --git a/scripts/library/size_to_human.sh b/scripts/library/size_to_human.sh new file mode 100644 index 00000000..9c0f0de2 --- /dev/null +++ b/scripts/library/size_to_human.sh @@ -0,0 +1,20 @@ +size_to_human() { + awk -v size="$1" ' + BEGIN { + suffix[1] = "B" + suffix[2] = "KiB" + suffix[3] = "MiB" + suffix[4] = "GiB" + suffix[5] = "TiB" + count = 1 + + while (size > 1024) { + size /= 1024 + count++ + } + + sizestr = sprintf("%.2f", size) + sub(/\.?0+$/, "", sizestr) + printf("%s %s", sizestr, suffix[count]) + }' +} |