diff options
author | Luke Bratch <l_bratch@yahoo.co.uk> | 2014-08-04 11:20:56 +0100 |
---|---|---|
committer | Luke Bratch <l_bratch@yahoo.co.uk> | 2014-08-04 11:20:56 +0100 |
commit | 7d57256a8b1527bb77aa683137655b92dbd97f90 (patch) | |
tree | f22140fd83ce4caf17adb6f441510cdc55868662 | |
parent | efe386d5a8037c6e5f59ecdd026d478a5b919b11 (diff) |
Move filesize formatting to a function rather than a separate bash
script
-rwxr-xr-x | head.bash | 15 |
1 files changed, 14 insertions, 1 deletions
@@ -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 |