This repository has been archived by the owner on May 17, 2019. It is now read-only.
/
lib.sh
143 lines (122 loc) · 4.86 KB
/
lib.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#!/bin/bash
#
# remot3.it Shell Script Lib - Just a simple library of handy shell script functions
#
# mike@remot3.it
#
#
# Notes
#
#${var#*SubStr} # will drop begin of string upto first occur of `SubStr`
#${var##*SubStr} # will drop begin of string upto last occur of `SubStr`
#${var%SubStr*} # will drop part of string from last occur of `SubStr` to the end
#${var%%SubStr*} # will drop part of string from first occur of `SubStr` to the end
#produces a unix timestamp to the output
utime()
{
echo $(date +%s)
}
#
# Produce a sortable timestamp that is year/month/day/timeofday
#
timestamp()
{
echo $(date +%Y%m%d%H%M%S)
}
#
# Simple Long Random
#
srand()
{
echo "$RANDOM$RANDOM"
}
#
# dev_random() - produces a crypto secure random number string ($1 digits) to the output (supports upto 50 digits for now)
#
# ret=dev_random(10)
#
dev_random()
{
local count=$1
if [ "$count" -lt 1 ] || [ "$count" -ge 50 ]; then
count=50;
fi
ret=$(cat /dev/urandom | tr -cd '0-9' | dd bs=$count count= 2>/dev/null)
echo -n "$ret"
}
# XML parse,: get the value from key $2 in buffer $1, this is simple no nesting allowed
#
xmlval()
{
temp=`echo $1 | awk '!/<.*>/' RS="<"$2">|</"$2">"`
echo ${temp##*|}
}
#
# JSON parse (very simplistic): get value frome key $2 in buffer $1, values or keys must not have the characters {}[",
# and the key must not have : in it
#
# Example:
# value=$(jsonval "$json_buffer" "$key")
#
jsonval()
{
temp=`echo "$1" | sed -e 's/[{}\"]//g' | sed -e 's/,/\'$'\n''/g' | grep -w $2 | cut -d"[" -f2- | cut -d":" -f2-`
#echo ${temp##*|}
echo ${temp}
}
jsonvalx()
{
temp=`echo $1 | sed -e 's/[{}"]//g' -e "s/,/\\$liblf/g" | grep -w $2 | cut -d":" -f2-`
#echo ${temp##*|}
echo ${temp}
}
#
# rem_spaces $1 - replace space with underscore (_)
#
rem_spaces()
{
echo "$@" | sed -e 's/ /_/g'
}
#
# rem_spaces $1 - replace space with underscore (^)
#
spaces2pipe()
{
echo "$@" | sed -e 's/ /^/g'
}
#
# rem_spaces $1 - replace ^ with space ( )
#
pipe2space()
{
echo "$@" | sed -e 's/^/ /g'
}
#
# urlencode $1
#
urlencode()
{
#STR="$1"
STR="$@"
[ "${STR}x" == "x" ] && { STR="$(cat -)"; }
echo ${STR} | sed -e 's| |%20|g' \
-e 's|!|%21|g' \
-e 's|#|%23|g' \
-e 's|\$|%24|g' \
-e 's|%|%25|g' \
-e 's|&|%26|g' \
-e "s|'|%27|g" \
-e 's|(|%28|g' \
-e 's|)|%29|g' \
-e 's|*|%2A|g' \
-e 's|+|%2B|g' \
-e 's|,|%2C|g' \
-e 's|/|%2F|g' \
-e 's|:|%3A|g' \
-e 's|;|%3B|g' \
-e 's|=|%3D|g' \
-e 's|?|%3F|g' \
-e 's|@|%40|g' \
-e 's|\[|%5B|g' \
-e 's|]|%5D|g'
}