-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutil.sh
110 lines (65 loc) · 1.59 KB
/
util.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
function check_path {
state=$1
pathvar=$2
if [[ ($state != "exists") && ($state != "not_exists") ]]; then
(>&2 echo "Unknown state: $state")
exit 1
fi
tstamp=`date`
if [[ $state == "exists" && ! -a "$pathvar" ]]; then
message="not created / does not exist."
(>&2 echo "[$tstamp] ${pathvar}" $message "Exiting.")
exit 1
fi
if [[ $state == "not_exists" && -a "$pathvar" ]]; then
message="exists."
(>&2 echo "[$tstamp] ${pathvar} $message")
while true; do
read -p "Overwrite? (Y/N)" yn
case $yn in
[Yy]* ) return 1 ;;
[Nn]* ) return 2 ;;
* ) echo "Please answer yes or no.";;
esac
done
fi
return 0
}
function info {
tstamp=`date`
(>&2 echo "[$tstamp] $1")
}
function compare_md5() {
A=$1
B=$2
MD5_A=`md5sum ${A} | cut -f1 -d' '`
MD5_B=`md5sum ${B} | cut -f1 -d' '`
if [[ $MD5_A != $MD5_B ]]; then
tstamp=`date`
(>&2 echo "[$tstamp] md5 check failed: ${A} and ${B} are different")
exit 1
fi
}
function create_and_check_dir () {
dname=$1
mkdir -p ${dname}
check_path exists ${dname}
echo `readlink -e $dname`
}
function move_and_check_file () {
src=$1
dst=$2
info "Moving ${src} to ${dst}"
rsync -a ${src} ${dst}
compare_md5 ${src} ${dst}
rm ${src}
}
function check_cmd_exists () {
# This doesn't catch the case where the cmd is a python command that is
# available in a different pyenv environment.
cmdstr=$1
eval type $cmdstr >/dev/null 2>&1 || {
echo >&2 "$cmdstr not installed. Aborting."
exit 1
}
}