diff options
| author | B. Watson <urchlay@slackware.uk> | 2025-12-27 02:15:34 -0500 |
|---|---|---|
| committer | B. Watson <urchlay@slackware.uk> | 2025-12-27 02:15:34 -0500 |
| commit | 97bcf3b08aa36dee23d9a34ed4ff75e0ed755c1a (patch) | |
| tree | 7e53bf06845864e4ed256548bed22f17de664111 | |
| parent | ac606a0509f82cfed713b41129b1e3fe1597ce83 (diff) | |
| download | misc-scripts-97bcf3b08aa36dee23d9a34ed4ff75e0ed755c1a.tar.gz | |
| -rwxr-xr-x | termbin | 44 |
1 files changed, 27 insertions, 17 deletions
@@ -1,16 +1,25 @@ -#!/bin/sh +#!/bin/bash + +VERSION="0.1.0" +SELF="$( basename $0 )" # 20200424 bkw: This used to be a one-line script: # cat "${1:--}" | nc termbin.com 9999 # ...but I really wanted the "copy link to X clipboard" feature. -if [ "$1" = "--help" ]; then +if [ "$1" = "--help" -o "$1" = "-h" ]; then cat <<EOF -Usage: $( basename $0 ) <filename> +termbin v$VERSION + +Usage: $SELF <filename> [<filename> ...] + +Uses nc (netcat) to paste stdin to termbin.com, or pastes one or more +files if <filename(s)> are given. Spits out paste URL on stdout. Also +copies URL to the X clipboard if X is running and either xsel or xclip +is installed. -Uses nc (NetCat) to paste stdin to termbin.com, or pastes a file if -<filename> is given. Spits out paste URL on stdout. Also copies URL to -the X clipboard if X is running and either xsel or xclip is installed. +Only one paste is created. If you give multiple filenames, they are +concatenated together. Written by B. Watson <urchlay@slackware.uk>, released under the WTFPL: do WTF you want with this. @@ -18,20 +27,21 @@ EOF exit 0 fi -if ! which nc &>/dev/null; then - echo "$( basename $0 ): can't find nc on path, install nc or netcat package" - exit 1 -fi +# 20251226 bkw: the sed is because termbin.com sends us e.g. +# https://termbin.com/XXXX\n\0 +# and we get "warning: ignoring null byte" from bash. +url="$( cat "${1:--}" | nc termbin.com 9999 | sed 's,\x00,,' )" -url="$( cat "${1:--}" | nc termbin.com 9999 )" -[ -z "$url" ] && exit "$?" +# 20251227 bkw: don't print an error message if nc fails (nc will print +# its own, or the shell will if it's "command not found"). +err="$?" +[ -z "$url" ] && exit "$err" echo "$url" if [ -n "$DISPLAY" ]; then - if which xsel &>/dev/null; then - echo -n "$url" | xsel -i - elif which xclip &>/dev/null; then - echo -n "$url" | xclip - fi + echo -n "$url" | xsel -i >/dev/null 2>&1 || \ + echo -n "$url" | xclip >/dev/null 2>&1 fi + +exit 0 |
