Poor Man’s Pastebin

I was going to add a pastebin to my site (for reasons I can not determine other than “because”), but that ended up being too much effort. Then I remembered that GNU Source Highlight can output HTML… So here is my pastebin client in less than ten lines of bash!

#!/bin/bash
 
file=$(mktemp ~/web/paste/XXXXXX)
 
[[ ! -z "$1" ]] && lang="-s $1"
 
cat - > ${file}.in
source-highlight -i ${file}.in -o ${file} ${lang}
rm ${file}.in
 
lftp -e "put -O /dir/to/paste ${file}; bye"
     -u user,password sftp://example.com
 
echo "http://example.com/paste/${file##*/}"

Done. Just use “foo | pastebin” to send the output of foo to the web. Or for files, use “pastebin < file". If auto-detection of highlighting style is bad, just use "pastebin <lang>" with one of the supported languages. The seemingly wasteful cat is because GNU source highlight can not autodetect the style from a pipe if it is not provided.

5 thoughts on “Poor Man’s Pastebin

  1. I do the same with my own pastebin implementation https://github.com/abique/paste-binouse

    And I have the following bash function to past:

    function paste-binouse-send()
    (
    host=”$1″
    src=”$2″
    temp=0

    if [[ -z “$src” || “$src” = “-” ]] ; then
    src=$(mktemp)
    cat >”$src”
    temp=1
    fi

    if [[ ! -r “$src” ]] ; then
    return
    fi

    mime_type=$(file –mime-type “$src” | cut -f 2 -d ‘ ‘)
    hdr=$(mktemp)
    curl -D “$hdr” –data-urlencode “content-type=${mime_type}”
    –data-urlencode “content@${src}” “$host”

    echo “$host”$(sed -n ‘s/location: (.*)/1/p’ “$hdr”)
    rm -rf “$hdr”
    if [[ $temp -eq 1 ]] ; then
    rm -f “$src”
    fi
    )

  2. Oh but bash is so slow. I want moaarrr speeeed. Use C instead ;)

  3. Thank you for the idea! I never thought to access pastebin with a script. Actually now I’m thinking about
    accessing other website with scripts. I’m interested by your script because I could use pastebin in
    the future. Just one question: why did you choose a filename of the form

    mktemp ~/web/paste/XXXXXX

    ? Is it because it’s required by pastebin ? Can’t we use for example

    ~/web/paste/my_file

    ?

    • This is not a script for pastebin.com or and publicly available pastebin service. There are plenty of those available already.

      And you can pick any directory you want. That is just where I store my web files.