-
Notifications
You must be signed in to change notification settings - Fork 0
/
myL.sh
executable file
·61 lines (51 loc) · 1.23 KB
/
myL.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
#!/bin/bash
## Shorten url or anything with files
## Usage:
## Shorten link:
## $0 <url link or long string>
## Expand link:
## $0 <L://linkstring>
## Directory store the links,
workdir='/run/shm/myL'
## Algorithm to shorten string
shorten='/usr/bin/sha1sum'
## Shorten to number of character, may auto increase
nw=5
## default prefix is 'L://'
prefix='L://'
arg="$*"
mkdir -p "$workdir"
## check input
if [[ "${arg}" == "${prefix}"* ]] ; then
#### if L:// => expend link
dst=${arg#"$prefix"}
if [ -f "${workdir}/${dst}" ]; then
str=$(cat "${workdir}/${dst}")
echo -n "$str"
exit 0
else
exit 1
fi
else
#### if not L:// => shorten string
n=0
while true ; do
ustr=$(echo -n "${arg}" | $shorten | cut -c -${nw})
if [ -f "${workdir}/${ustr}" ]; then
n=$(expr $n + 1)
if [ "${arg}" == "$(cat ${workdir}/${ustr})" ]; then
echo -n "${prefix}${ustr}"
exit
else
nw=$(expr $nw + 1)
continue
fi
else
dst="${ustr}"
break
fi
done
echo -n "${arg}" > ${workdir}/${dst}
echo -n "${prefix}${dst}"
exit 0
fi