From 7d57256a8b1527bb77aa683137655b92dbd97f90 Mon Sep 17 00:00:00 2001 From: Luke Bratch Date: Mon, 4 Aug 2014 11:20:56 +0100 Subject: Move filesize formatting to a function rather than a separate bash script --- head.bash | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) 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 -- cgit v1.2.3