summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLuke Bratch <l_bratch@yahoo.co.uk>2014-08-04 11:20:56 +0100
committerLuke Bratch <l_bratch@yahoo.co.uk>2014-08-04 11:20:56 +0100
commit7d57256a8b1527bb77aa683137655b92dbd97f90 (patch)
treef22140fd83ce4caf17adb6f441510cdc55868662
parentefe386d5a8037c6e5f59ecdd026d478a5b919b11 (diff)
Move filesize formatting to a function rather than a separate bash
script
-rwxr-xr-xhead.bash15
1 files changed, 14 insertions, 1 deletions
diff --git a/head.bash b/head.bash
index 9cc425d..71f8baf 100755
--- a/head.bash
+++ b/head.bash
@@ -1,5 +1,17 @@
#!/bin/bash
+function filesize {
+echo $1 | awk 'function human(x) {
+ s=" B KiB MiB GiB TiB EiB PiB YiB ZiB"
+ while (x>=1024 && length(s)>1)
+ {x/=1024; s=substr(s,5)}
+ s=substr(s,1,4)
+ xf=(s==" B ")?"%5d ":"%8.2f"
+ return sprintf( xf"%s\n", x, s)
+ }
+ {gsub(/^[0-9]+/, human($1)); print}'
+}
+
read url
arg1=`echo "$url" | cut -d\ -f1`
argrest=`echo "$url" | cut -d\ -f1 --complement`
@@ -24,7 +36,8 @@ sed -i ':a;N;$!ba;s/\r//g' head.txt
http=`grep HTTP head.txt`
modified=`grep Last-Modified head.txt | sed "s/Last-Modified: //"`
type=`grep Content-Type head.txt | cut -d " " -f2`
-length=`grep Content-Length head.txt | cut -d " " -f2 | ./filesize.sh | sed -r "s/[ \t]{2,}//"`
+length=`grep Content-Length head.txt | cut -d " " -f2`
+length=`filesize $length | sed -r "s/[ \t]{2,}//"`
if [[ ! "$http" =~ "200" ]]
then