Improvements:
TestStage() {
local url sum_web sum_loc
url="distfiles.gentoo.org/releases/${1:7:5}/current-stage3/${1}"
wget -q "$url.DIGESTS"
{ read; read -r sum_web; } < "$1.DIGESTS"
sum_loc=$(openssl dgst -r -sha512 "$1")
####### time to return
[[ "${sum_web:0:128}" = "${sum_loc:0:128}" ]]
return
}
- Use of lower case variable names.
- No use of the deprecated
function
keyword.
- Use of
$(...)
instead of backticks.
- Use of bash builtins instead of
sed
to get the second line of file "$1.DIGESTS"
. This saves a process spawn and a subshell (and a useless use of cat).
return
on its own will return the return code of the previous statement, here the test statement.
- Declare all local variables at once.
If you don't care about the file $1.DIGESTS
that will be saved, you can also do:
TestStage() {
local url sum_web sum_loc
url="distfiles.gentoo.org/releases/${1:7:5}/current-stage3/${1}"
{ read; read -r sum_web; } < <(wget -q -O- "$url.DIGESTS")
sum_loc=$(openssl dgst -r -sha512 "$1")
####### time to return
[[ "${sum_web:0:128}" = "${sum_loc:0:128}" ]]
return
}
Now, "${1:7:5}"
will, as I understand it, expand to the second field of stage3-<arch>-<release>.tar.bz2
(where fields are separated by hyphens). You could also do:
IFS=- read _ arch _ <<< "$1"
In this case, your function would be:
TestStage() {
local arch url sum_web sum_loc
IFS=- read _ arch _ <<< "$1"
url="distfiles.gentoo.org/releases/$arch/current-stage3/${1}"
{ read; read -r sum_web; } < <(wget -q -O- "$url.DIGESTS")
sum_loc=$(openssl dgst -r -sha512 "$1")
####### time to return
[[ "${sum_web:0:128}" = "${sum_loc:0:128}" ]]
return
}
Hope this helps.
Then, use as:
if TestStage "stage3-<arch>-<release>.tar.bz2"; then
# return value is true, proceed accordingly
else
# return value is false, proceed accordingly
fi