aboutsummaryrefslogtreecommitdiff
path: root/termbin
blob: 6a9f3046b018219436cac9d0a310615857087570 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/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" -o "$1" = "-h" ]; then
	cat <<EOF
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.

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.
EOF
	exit 0
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,,' )"

# 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
	echo -n "$url" | xsel -i >/dev/null 2>&1 || \
	echo -n "$url" | xclip   >/dev/null 2>&1
fi

exit 0