aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xtermbin44
1 files changed, 27 insertions, 17 deletions
diff --git a/termbin b/termbin
index 441be32..6a9f304 100755
--- a/termbin
+++ b/termbin
@@ -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