Posted By

ksaver on 08/24/10


Tagged

url Bash short shorten


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

smvnet
tazjel


Shorten a long URL from bash command line


 / Published in: Bash
 

URL: http://identi.ca/ksaver

Usage example:\r\n$./ur1ca.sh \\"http://www.google.com/search?q=bash+scripting\\"\r\nYour ur1 is: http://ur1.ca/17xpn\r\n$

  1. #! /usr/bin/env bash
  2. # ur1ca.sh
  3. # Shorten a long URL from bash command line,
  4. # using ur1.ca open source service.
  5. # ksaver (at identi.ca), Aug 2010.
  6. # Public Domain Code.
  7. # No warranty at all.
  8.  
  9. function _curl()
  10. {
  11. /usr/bin/env curl -s -A 'Mozilla' "$@"
  12. }
  13.  
  14. function html_txt()
  15. {
  16. read string
  17. echo "$string" |sed -e 's/<[^>]*>//g'
  18. }
  19.  
  20. function usage()
  21. {
  22. prname=$(basename $0)
  23. echo -e "\tUsage: $prname <long-url>"
  24. echo -e "\tExamp: $prname http://www.google.com/search?q=bash+scripting"
  25. }
  26.  
  27. function _main_()
  28. {
  29. if [ -z "$1" ]
  30. then
  31. usage
  32. exit
  33. else
  34. UR1='http://ur1.ca/'
  35. LONG="$1"
  36.  
  37. _curl -s "$UR1" -d"longurl=$LONG" |grep -i 'Your ur1 is' |html_txt
  38. echo
  39. fi
  40. }
  41.  
  42. ## Run script...
  43. _main_ "$@"

Report this snippet  

You need to login to post a comment.