-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathfunction
190 lines (164 loc) · 3.83 KB
/
function
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#=============================================
# send mail to somebody
# arg1: mail head
# arg2: mail contents
# arg3: mail address
#
# return:
# 0 : OK
# 1 : ERROR
function send_mail()
{
if [ -n "$1" -a -n "$3" ]; then
echo "$2" | mail -s "$1" "$3";
return $?;
fi
return 1;
}
#=============================================
# cp file from src to dest.
# arg1: src
# arg2: dest
#
# return:
# 0 : OK
# 1 : ERROR
function cp_file()
{
if [ -n "$1" -a -n "$2" ]; then
if [ "$1" != "$2" ]; then
cp -f $1 $2;
return $?
else
return 1;
fi
fi
return 1;
}
#=============================================
# cp files from src/ to dest/.
# arg1: src/
# arg2: dest/
#
# return:
# 0 : OK
# 1 : ERROR
function cp_files()
{
if [ -n "$1" -a -n "$2" ]; then
if [ "$1" != "$2" ]; then
cp -r -b "$1/*" "$2";
return $?
else
return 1;
fi
fi
return 1;
}
#=============================================
# mv file from src to dest.
# arg1: src
# arg2: dest
#
# return:
# 0 : OK
# 1 : ERROR
function mv_file()
{
if [ -n "$1" -a -n "$2" ]; then
if [ "$1" != "$2" ]; then
mv "$1" "$2";
return $?
else
return 1;
fi
fi
return 1;
}
SEPERATOR='/';
#=============================================
# detect the path string have a tail slash.
# arg1: path
#
# return:
# 0 : NO
# 1 : YES
function have_tail_slash()
{
local path=$1;
if [ -n $path ]; then
local len=`expr ${#path} - 1`;
local ch=${path:$len:1};
if [ $ch = $SEPERATOR ]; then
return 1;
else
return 0;
fi
else
return 0;
fi
}
MD5="/usr/bin/md5sum"
function validate_checksum() {
cksum=`$MD5 "$1" | sed 's/.*\([0-9a-fA-F]\{32\}\).*/\1/'`
valid=`< "$srcdir/${1%-t}.md5"`
test "x$cksum" = "x$valid"
}
G_LOG_MIN_LINE_NUM=1000
G_LOG_MAX_LINE_NUM=10000
G_PID=$$
function clean_log
{
local log_file="${G_LOG_FILE}"
local current_line=0
current_line=`wc -l $log_file | awk '{print $1}'`
if [ $current_line -ge $G_LOG_MAX_LINE_NUM ]
then
tail -${G_LOG_MIN_LINE_NUM} ${log_file} >${log_file}.${G_PID}
mv ${log_file}.${G_PID} ${log_file}
fi
}
function exesudo() {
#
# I use underscores to remember it's been passed
local _funcname_="$1"
local params=( "$@" ) ## array containing all params passed here
local tmpfile="/dev/shm/$RANDOM" ## temporary file
local filecontent ## content of the temporary file
local regex ## regular expression
local func ## function source
#
# Shift the first param (which is the name of the function)
unset params[0] ## remove first element
# params=( "${params[@]}" ) ## repack array
#
# WORKING ON THE TEMPORARY FILE:
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
content="#!/bin/bash\n\n"
#
# Write the params array
content="${content}params=(\n"
regex="\s+"
for param in "${params[@]}"
do
if [[ "$param" =~ $regex ]]
then
content="${content}\t\"${param}\"\n"
else
content="${content}\t${param}\n"
fi
done
content="$content)\n"
echo -e "$content" > "$tmpfile"
#
# Append the function source
echo "#$( type "$_funcname_" )" >> "$tmpfile"
#
# Append the call to the function
echo -e "\n$_funcname_ \"\${params[@]}\"\n" >> "$tmpfile"
#
# DONE: EXECUTE THE TEMPORARY FILE WITH SUDO
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
sudo bash "$tmpfile"
rm "$tmpfile"
}