Skip to content

Instantly share code, notes, and snippets.

@diffficult
Created April 9, 2017 03:42
Show Gist options
  • Save diffficult/d8d95ceb1db15c8892576de8aabbef1a to your computer and use it in GitHub Desktop.
Save diffficult/d8d95ceb1db15c8892576de8aabbef1a to your computer and use it in GitHub Desktop.
Small script written by someone that outputs as a QR code whatever you have on your clipboard at the time
#!/bin/bash
#
# This script will output a QR Code made with whatever you have loaded on your clipboard
# You need qrencode C Library and xclip for this script to work
#
function err {
echo -e "[ERROR] in line ${BASH_LINENO[0]}"
[[ $1 ]] && echo "[ MSG ] $1"
exit 1
}
function printBold {
echo -e "\n$(tput bold)\"$1\"$(tput sgr0)\n"
}
hash qrencode || err "qrencode not found."
hash xclip || err "xclip not found."
if [[ ! -z $1 ]]; then
# if given, use arguments as target
TARGET="$@"
else
# per default, get target from clipboard.
TARGET="$( xclip -selection c -o )" || err
[[ ! -z $TARGET ]] || err "clipboard empty."
fi
printBold "$TARGET"
QRCODE="$( qrencode -t UTF8 "$TARGET" )" || err
echo "$QRCODE"
# invert block characters. resulting in a qr-code readable on a light background.
QRCODE="${QRCODE//' '/'0'}"
QRCODE="${QRCODE//'█'/'1'}"
QRCODE="${QRCODE//'▄'/'2'}"
QRCODE="${QRCODE//'▀'/'3'}"
QRCODE="${QRCODE//'3'/'▄'}"
QRCODE="${QRCODE//'2'/'▀'}"
QRCODE="${QRCODE//'1'/' '}"
QRCODE="${QRCODE//'0'/'█'}"
# comment the previous block and uncomment the next one, when in need of
# block sized characters instead of whitespaces (e.g to paste to no-monospace environment).
#QRCODE="${QRCODE//'3'/'▄'}"
#QRCODE="${QRCODE//'2'/'▀'}"
#QRCODE="${QRCODE//'1'/'░'}"
#QRCODE="${QRCODE//'0'/'█'}"
echo "$QRCODE"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment