Skip to content

Instantly share code, notes, and snippets.

@habibutsu
Created April 19, 2013 14:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save habibutsu/5420781 to your computer and use it in GitHub Desktop.
Save habibutsu/5420781 to your computer and use it in GitHub Desktop.
Output the last part of files via http
#!/bin/bash
if [ -z "$1" ]; then
echo "Usage $0 <url to file>"
exit 1
fi
BASE_URL=$(dirname $1)
FILE=$(basename $1)
FILE_LIST=$(lynx -dump -nolist -notitle "${BASE_URL}")
SIZE=$(echo "${FILE_LIST}"|sed -n -e "/$FILE/{n;p;}"|egrep -o "[0-9]+")
START_SIZE=$(expr $SIZE - 1000)
while [ true ]
do
STATUS=$(curl -s -I --range $START_SIZE-$SIZE $1)
STATUS_CODE=$(echo -e "${STATUS}"|egrep "HTTP/1.1 [0-9]+"|egrep -o "[0-9]{3}")
CONTENT_LENGTH=$(echo -e "${STATUS}"|egrep "Content-Length: [0-9]+"|egrep -o "[0-9]+")
if [ $STATUS_CODE == 206 ]; then
DATA=$(curl -s --range $START_SIZE-$SIZE $1)
echo -e "${DATA}"
START_SIZE=$(expr $START_SIZE + $CONTENT_LENGTH)
SIZE=$(expr $START_SIZE + 1000)
fi
sleep 1
done
@adyp
Copy link

adyp commented Jan 6, 2014

Checkout improved version (no lynx dependency, supports authentication, dynamically sized chunks) and with "tail" like cmd line options in this fork: https://gist.github.com/bsdcon/7224196

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment