-
Notifications
You must be signed in to change notification settings - Fork 0
/
sprunge
executable file
·90 lines (73 loc) · 2.43 KB
/
sprunge
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/bin/bash
# Essentially http://www.shellperson.net/sprunge-pastebin-script/
printusage() {
usage | fmt -s >&2
}
usage() {
cat <<EOF
USAGE
$0 [OPTION] filename.txt
$0 [OPTION] text string
$0 [OPTION] < filename.txt
piped_data | $0 [OPTION]
Try '$0 --help' for more information.
EOF
}
printhelp() {
description | fmt -s >&2
}
description() {
cat <<EOF
DESCRIPTION
Upload data and fetch URL from the pastebin http://sprunge.us
USAGE
$0 [OPTION] filename.txt
$0 [OPTION] text string
$0 [OPTION] < filename.txt
piped_data | $0 [OPTION]
OPTIONS
-h, --help print this help.
-c, --copy copy sprunge link to X clipboard.
NOTES
--------------------------------------------------------------------------
* INPUT METHODS *
$0 can accept piped data, STDIN redirection [<filename.txt], text strings following the command as arguments, or filenames as arguments. Only one of these methods can be used at a time, so please see the note on precedence.
* PRECEDENCE *
STDIN redirection has precedence, then piped input, then a filename as an argument, and finally text strings as an arguments.
EXAMPLE:
echo piped | "$0" arguments.txt < stdin_redirection.txt
In this example, the contents of file_as_stdin_redirection.txt would be uploaded. Both the piped_text and the file_as_argument.txt are ignored. If there is piped input and arguments, the arguments will be ignored, and the piped input uploaded.
* FILENAMES *
If a filename is misspelled or doesn't have the necessary path description, it will NOT generate an error, but will instead treat it as a text string and upload it.
--------------------------------------------------------------------------
EOF
exit
}
run() {
if [ -t 0 ]; then
echo Running interactively, checking for arguments... >&2
if [ "$*" ]; then
echo Arguments present... >&2
if [ -f "$*" ]; then
echo Uploading the contents of "$*"... >&2
cat "$*"
else
echo Uploading the text: \""$*"\"... >&2
echo "$*"
fi | curl -F 'sprunge=<-' http://sprunge.us
else
echo No arguments found, printing USAGE and exiting. >&2
printusage
fi
else
echo Using input from a pipe or STDIN redirection... >&2
while read -r line ; do
echo $line
done | curl -F 'sprunge=<-' http://sprunge.us
fi
}
case "$1" in
-h|--help) printhelp ;;
-c|--copy) run "${@:2}" | xclip -sel clip; echo "Copied sprunge link to clipboard." ;;
*) run "$@" ;;
esac