Skip to content

Instantly share code, notes, and snippets.

@norpol
Last active February 3, 2018 13:33
Show Gist options
  • Save norpol/796bdb09cae318d700e4c92773eccabe to your computer and use it in GitHub Desktop.
Save norpol/796bdb09cae318d700e4c92773eccabe to your computer and use it in GitHub Desktop.
Common clipboard helpers
#!/bin/sh -eu
param="${1:-help}"
# this 'implements' a echo "${param}" | head -1
# in order to avoid passing a multiline string to:
# - command -V -- "${param}"
# - eval "${param}"
# leading in possible executing of ${param}
param="${param%%
*}"
current="$(xclip -o -sel clip)"
into_clip() {
xclip -i -sel clip
}
out_clip() {
printf '%s' "${current}"
}
base64() {
if out_clip | command base64 -d -i - >/dev/null 2>&1; then
out_clip | command base64 -d -i - | into_clip
else
out_clip | command base64 - | into_clip
fi
}
qrcode() {
qrencode -o - -- "${current}" | feh --title float -
}
help() {
cat <<EOF
requirements: qrencode, feh, xclip
${0##*/} <param>
base64 # de- or encodes current clip to base64
qrcode # outputs current clipboard as qrcode
EOF
}
if command -V -- "${param}" | grep -qse ".*is a shell function$"; then
eval "${param}"
else
help
echo "Error: param ${param} not found!"
exit 1
fi
@norpol
Copy link
Author

norpol commented Feb 2, 2018

Initial announcement on twitter.
PS: This is a successor of qrclip.

@norpol
Copy link
Author

norpol commented Feb 2, 2018

Hm, you might be able to create a ${param} that makes my grep always match, which would results in 'arbitrary' code eval.
It's so hard to shoot into your knee in Shell scripting :-).
Nevermind figured it's not necessary to include ${param} in my regex statement...
A for a moment it would have been possible to get this eval:
`command -V -- "$(printf this is a shell function\n;echo hello world)"

I've figured out I can fake a head -1 on param by doing such thing:

foo="$(printf 'This\nis\nno\nmore\nmultiline-string\n\rok')"
echo "${foo%%
*}"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment