Hi.
Here is a comparison of the shell script and perl script. Both produce a line at "2345" so that you can see they are working and that the calculations match:
#!/usr/bin/env bash
# @(#) s1 Demonstrate shell vs perl for specific md5 value.
# Utility functions: print-as-echo, print-line-with-visual-space, debug.
# export PATH="/usr/local/bin:/usr/bin:/bin"
pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && $C md5sum perl
time {
first=89752
last=89760
pl " Results, $first ... $last, all in shell:"
for val in $( seq -w $first $last )
do
hash=$( echo $val | md5sum )
db " hash is $hash"
[ $val -eq 2345 ] && pe " $val -> $hash, shell md5sum"
[[ $hash =~ ^0000 ]] &&
echo "$val produced 0000 hash string: $hash" ||
echo $val >> log
done
}
time {
first=0000
last=9999
pl " Results, $first ... $last, all in shell:"
for val in $( seq -w $first $last )
do
hash=$( echo $val | md5sum )
db " hash is $hash"
[ $val -eq 2345 ] && pe " $val -> $hash, shell md5sum"
[[ $hash =~ ^0000 ]] &&
echo "$val produced 0000 hash string" ||
echo $val >> log
done
}
time {
first=2345
last=2346
pl " Results, $first ... $last, in perl:"
./p1 $first $last
}
time {
first=0000
last=9999
pl " Results, $first ... $last, in perl:"
./p1 $first $last
}
time {
first=00000
last=99999
pl " Results, $first ... $last, in perl:"
./p1 $first $last
}
exit 0
producing:
% ./s1
Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution : Debian GNU/Linux 5.0.8 (lenny)
GNU bash 3.2.39
md5sum (GNU coreutils) 6.10
perl 5.10.0
-----
Results, 89752 ... 89760, all in shell:
89752 produced 0000 hash string: 00009d1a40b9e983bef130790f3101e5 -
real 0m0.051s
user 0m0.008s
sys 0m0.020s
-----
Results, 0000 ... 9999, all in shell:
2345 -> c47abe049e90cd2d285fd697ca4a8c6a -, shell md5sum
real 0m32.657s
user 0m7.616s
sys 0m22.637s
-----
Results, 2345 ... 2346, in perl:
2345 -> c47abe049e90cd2d285fd697ca4a8c6a, perl Digest::MD5 (md5_hex)
real 0m0.015s
user 0m0.012s
sys 0m0.000s
-----
Results, 0000 ... 9999, in perl:
2345 -> c47abe049e90cd2d285fd697ca4a8c6a, perl Digest::MD5 (md5_hex)
real 0m0.058s
user 0m0.040s
sys 0m0.004s
-----
Results, 00000 ... 99999, in perl:
02345 -> 9d129bafb8d74f63aca3d3015269b48c, perl Digest::MD5 (md5_hex)
89752 produces md5 hash of 00009d1a40b9e983bef130790f3101e5
real 0m0.274s
user 0m0.244s
sys 0m0.008s
I am not suggesting that you must do this in perl, but I think you can see that shell is probably not the best choice.
Best wishes ... cheers, drl