/
md5
executable file
·152 lines (141 loc) · 2.69 KB
/
md5
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
#!/usr/bin/env bash
#
# ## variable_is_nonempty()
#
# Test to see if a variable is empty.
#
# ### Input Parameters
#
# First parameter is a string containing a variable name.
#
# ### Stream Outputs
#
# None.
#
# ### Environmental effects
#
# None.
#
# ### Return Codes
#
# 0 for success.
#
# ### Failure Scenarios
#
# Fails if no variable name was given as the first argument.
#
# ### Usage Examples
#
# user$ variable_is_nonempty asdf
# user$ echo $?
# 1
#
# user$ asdf="w00t! "
# user$ variable_is_nonempty asdf
# user$ echo $?
# 0
#
variable_is_nonempty()
{
# Store the first parameter, which should be the variable name, in the
# variable variable :)
local _variable="${1:-}"
# If the variable name is nonempty
if [[ -n "${_variable}" ]]
then
# If the evaluation of a nonempty test with the variable name used [huh???]
eval "[[ -n \"\${${_variable}:-}\" ]]"
else
# Otherwise no parameters were passed in; this is a programming error.
# Send a failure message which also triggers a backtrace enabling the
# developer to quickly pinpoint and fix their error.
fail "Cannot check if variable is nonempty; no variable was given."
fi
}
#
# ## command\_exists()
#
# Checks to see whether a command exists within the current environment and PATH.
#
# ### Input Parameters
#
# First parameter is a command name.
#
# ### Stream Outputs
#
# None.
#
# ### Environmental effects
#
# none.
#
# ### return codes
#
# 0 if the command was found in the current environment.
# 1 if the command was not found in the current environment.
#
# ### failure scenarios
#
# Fails if no command name was given.
#
# ### usage examples
#
# user$ command_exists adsf
# user$ echo $?
# 1
#
# user$ command_exists ls
# user$ echo $?
# 0
#
command_exists()
{
local _name="${1:-}"
if variable_is_nonempty _name
then
if command -v "${_name}" > /dev/null 2>&1
then
return 0
else
return 1
fi
else
fail "Cannot test if command exists; no command name was given."
fi
}
if (( ${rvm_trace_flag:=0} == 2 ))
then
set -x
export rvm_trace_flag
fi
_archive="${1}"
shift
md5="${1}"
shift
# Swiped from BDSM
if command_exists md5
then
archive_md5=$(md5 -q "${_archive}")
elif command_exists md5sum
then
archive_md5="$(md5sum "${archive}")"
archive_md5="${archive_md5%% *}"
else
for _path in /sbin /bin /usr/bin /usr/sbin
do
if [[ -x "${_path}/md5" ]]
then
archive_md5=$(${_path}/md5 -q "${_archive}")
elif [[ -x "${_path}/md5sum" ]]
then
archive_md5="$(${_path}/md5sum "${archive}")"
archive_md5="${archive_md5%% *}"
fi
done
fi
if [[ "${archive_md5}" == "${md5}" ]]
then
exit 0
else
exit 1
fi