flipje

paste2pastebin

Jul 8th, 2011
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.67 KB | None | 0 0
  1. #!/bin/bash
  2. # this tool is handy to copy standard output from the command line to pastebin
  3. # it wil return your output with an URL.
  4. # refurbished by flip hess 2011
  5. # originator: http://www.anildewani.com
  6. # coder : Anil Dewani
  7. # date : Novemeber 7, 2010
  8. # Paste at Pastebin.com using command line (browsers are slow, right?)
  9.  
  10.  
  11. # Global variables:
  12.  
  13.   PATH='/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin'
  14.   SCRIPT_PATH="${0}"
  15.  
  16.   # set empty var:
  17.   NAME=
  18.   EMAIL=
  19.   TYPE=
  20.  
  21.   API="http://pastebin.com/api_public.php"
  22.  
  23. # Functions:
  24.  
  25.   # exit function
  26.   function die()
  27.   {
  28.     echo "${1}"
  29.     exit 1
  30.   }
  31.  
  32.  
  33.   # The main function.
  34.   function fMain()
  35.   {
  36.     #get options and set vars
  37.     while getopts "n:e:t:h" OPTION
  38.     do
  39.       case ${OPTION} in
  40.        
  41.         n)
  42.           NAME=${OPTARG}
  43.           ;;
  44.         e)
  45.           EMAIL=${OPTARG}
  46.           ;;
  47.         t)
  48.           TYPE=${OPTARG}
  49.           ;;
  50.         h)
  51.           fShowUsage
  52.           ;;
  53.         ?)
  54.           fShowUsage
  55.           ;;
  56.       esac
  57.     done
  58.  
  59.     # check for stdin, if so, get it :)
  60.     if [ "$( tty )" == 'not a tty' ]
  61.     then
  62.       INPUT="$(</dev/stdin)"
  63.     else
  64.       fShowUsage
  65.     fi
  66.  
  67.     # check for empty input
  68.     [ -z ${INPUT} ] && fShowUsage
  69.  
  70.     QUERYSTRING="paste_private=0&paste_code=${INPUT}&paste_name=${NAME}&paste_email=${EMAIL}&paste_format=${TYPE}"
  71.  
  72.     #post data to pastebin.com API
  73.     curl -d "${QUERYSTRING}" "${API}"
  74.     echo ""
  75.  
  76.     return 0
  77.  
  78.   }
  79.  
  80.   # Shows usage.
  81.   function fShowUsage()
  82.   {
  83.     echo "Pastebin.com Bash Script"
  84.     echo ""
  85.     echo "Usage : ${SCRIPT_PATH} [ -n <paste name> ] [ -e <paste email> ] [ -t <type of code> ] [ -h ]"
  86.     echo ""
  87.     echo "   <paste name>   Specify the name of paste to be used (optional)"
  88.     echo "   <paste email>  Specify email to be used while pasting (optional)"
  89.     echo "   <type of code> Specify code language used, use any of the following values (optional and default value is plain text)"
  90.     echo ""
  91.     echo "Example: \"echo \"Testing\" | ./${SCRIPT_PATH}\""
  92.     echo ""
  93.     echo "=> Some famous [ -t <type of code> ] Values:"
  94.     echo ""
  95.     echo "  - php - PHP"
  96.     echo "  - actionscript3 - Action Script 3"
  97.     echo "  - asp - ASP"
  98.     echo "  - bash - BASH script"
  99.     echo "  - c - C language"
  100.     echo "  - csharp - C#"
  101.     echo "  - cpp - C++"
  102.     echo "  - java - JAVA"
  103.     echo "  - sql - SQL"
  104.     echo ""
  105.  
  106.     exit 1
  107.   }
  108.  
  109. # do the magic:
  110.  
  111.   # first things first: check for curl
  112.   [ -x "$(which curl)" ] || die "this script depends on curl"
  113.  
  114.   # Start the program:
  115.   fMain "${@}"
  116.  
  117.   # Exit with previous return code:
  118.   exit "${?}"
Advertisement
Add Comment
Please, Sign In to add comment