-
Notifications
You must be signed in to change notification settings - Fork 3
/
clip.sh
executable file
·88 lines (76 loc) · 1.05 KB
/
clip.sh
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
#!/bin/bash
READ=0
LAST_BYTE=""
if [ -z "$CLIP_FILE" ]; then
CLIP_FILE="$HOME/.clip"
fi
if [ -t 0 ]; then
READ=1
fi
debug () {
if ! [ -z "$DEBUG" ]; then
{
printf "clip: debug: "
echo "$@"
} >&2
fi
}
clip_empty () {
if [ -z "`cat $CLIP_FILE`" ]; then
return 0
else
LAST_BYTE="$byte"
return 1
fi
}
clip_exists () {
if test -f "$CLIP_FILE"; then
return 0
else
return 1
fi
}
clip_create () {
if ! clip_exists; then
touch "$CLIP_FILE"
fi
}
clip_destroy () {
if clip_exists; then
rm -f "$CLIP_FILE"
fi
}
clip_read () {
clip_create
if clip_empty; then
debug "empty"
return 1
else
cat "$CLIP_FILE" | {
while read line; do
debug "read '$line'"
echo $line
done
}
return 1
fi
}
clip_write () {
clip_create
while read -r line; do
debug "write '$line'"
echo "$line"
done >> "$CLIP_FILE"
}
_clip () {
if [ "1" == "$READ" ]; then
clip_read
return 0
else
clip_destroy
clip_write
return 0
fi
}
_clip
exit $?