summaryrefslogtreecommitdiff
path: root/head.bash
blob: aa27d806be7d8229e8ce16e396c7bde131dcf43e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/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`

if [[ $arg1 = "--version" ]]
then
  echo "Blatech head v0.2.2"
  exit
elif [[ $arg1 = "-h" || $arg1 = "--help" ]]
then
  echo "Give an HTTP URL to get its MIME type, size and modified date.  --vebose or -v for verbose output, --help or -h for this help message, --version for the version number."
  exit
elif [[ $arg1 = "-v" || $arg1 = "--verbose" ]]
then
  curl -sI "$argrest" | sed ':a;N;$!ba;s/\r\n/ | /g' | sed 's/ | \r$//'
  exit
fi

curl -sI "$url" > head.txt
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`
length=`filesize $length | sed -r "s/[ \t]{2,}//"`

if [[ ! "$http" =~ "200" ]]
then
  echo $http
else
  if [ -z "$length" ] && [ -z "$modified" ]
  then
    echo "$type"
  else
    echo "$type | $length | $modified"
  fi
fi

rm head.txt