Skip to content

Instantly share code, notes, and snippets.

@winny-
Created May 8, 2014 00:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save winny-/fe3c9b43059637584e54 to your computer and use it in GitHub Desktop.
Save winny-/fe3c9b43059637584e54 to your computer and use it in GitHub Desktop.
sprunge script supporting both curl and wget
#!/bin/sh
has_command() {
which "$1" >/dev/null 2>&1
}
urlencode() {
python -c '
import sys
import urllib
data = sys.stdin.read()
urlencoded = urllib.quote(data)
print(urlencoded)
'
}
if has_command curl; then
sprungecommand="curl -sS -F sprunge=<- http://sprunge.us"
elif has_command wget && has_command python; then
wget_wrapper() { wget -q -O - --post-data="sprunge=$(urlencode)" http://sprunge.us; }
sprungecommand="wget_wrapper"
else
printf 'No suitable HTTP utility found for sprunging! :(\n' >&2
exit 1
fi
sprunge() {
$sprungecommand
err=$?
if [ $err -ne 0 ]; then
printf 'Failed to sprunge %s :(\n' "$file" >&2
fi
return $err
}
if [ "X$1" = 'X' -o "X$1" = 'X-' ]; then
sprunge
else
for file in "$@"; do
# Become responsive if terminal.
if [ -t 1 ]; then
printf '%s: ' "$file"
else
[ "X$multiple" != 'X' ] && printf ' '
fi
sprunged="$(sprunge < "$file")"
err=$?
# Figure out file extension for url argument.
if printf "$file"|grep -qF '.'; then
url="$sprunged?$(printf "$file"|awk -F . '{print $NF}')"
else
url="$sprunged"
fi
if [ $err -eq 0 ]; then
printf '%s' "$url"
if [ -t 1 ]; then
echo
fi
fi
multiple=yes
done
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment