-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcommon.sh
executable file
·78 lines (68 loc) · 1.19 KB
/
common.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
#!/bin/bash
# coding: utf-8
# Import once
[ ${BJ_HAS_COMMON:-0} -ne 0 ] && return 0
# Decode Unicode escaped string to UTF-8 string
function decode()
{
sed -e 's/\\u\(....\)/\&#x\1;/g' | nkf --numchar-input
}
export -f decode
# Escape charactor
function escape()
{
sed -e 's/\\"/"/g' -e 's/\\\//\//g' -e 's/\\n/\n/g'
}
export -f escape
#
function unref_char()
{
sed \
-e 's/ / /g' \
-e 's/"/"/g' \
-e "s/'/'/g" \
-e 's/</</g' \
-e 's/>/>/g' \
-e 's/&/&/g'
}
export -f unref_char
# Strip HTML tags and remove spaces for indent
function strip_html_tag()
{
sed -e 's/<[^>]*>//g' -e 's/^\s\s*//' -e 's/\s\s*$//' | decode | unref_char
}
export -f strip_html_tag
# Check
function is_same()
{
if [ $(md5sum $1 $2 | cut -c -32 | uniq | wc -l) -eq 1 ]; then
return 0
else
return 1
fi
}
export -f is_same
#
function now()
{
date +"%Y-%m-%d_%H-%M-%S"
}
export -f now
#
function save_with_backup()
{
temp="$1"
dest="$2"
if [ -f "$dest" ]; then
is_same "$dest" "$temp"
if [ $? -eq 0 ]; then
rm "$temp"
else
cp "$temp" "$dest"
fi
else
mv "$temp" "$dest"
fi
}
export -f save_with_backup
export BJ_HAS_COMMON=1