Thanks Malcolm, that worked a treat

I also learned from the linuxformat forum that I need not have specified
$FILES as a variable at all, I could have just run:

for f in *.sh
...

which did the same thing as

FILES="`ls *.sh`"
for f in $FILES
...

Both work perfectly under Ubuntu AND CentOS.

Furthermore, for anyone interested in this thread I now have two complete
scripts that compliment each other, the first generates all the MD5 hashes,
the second compares all the files with their corresponding hash files:

 = md5all.sh =

FILES="`ls *.sh`"
for f in $FILES
do
md5sum $f > $f.md5
done

===========


and

= md5checkall.sh =

# Searches for $searchcriteria (line 6) and checks for an associated md5
hash, and compares the md5sum with the value stored in the .md5 file. Gives
a Pass or a Fail.

# variables for colors used in output text
red="\033[31m";    green="\033[32m";   reset="\033[0m";   yellow="\033[33m"

# variables
searchcriteria="*.sh"
files="`ls $searchcriteria`"

# main script
clear
echo "Checking md5sum(s) for all files matching: $searchcriteria."
echo ""
for f in $files
do
   gethash="`md5sum $f`"
   if [ -f $f.md5 ]
   then
      filehash="`cat $f.md5`"
   else
      echo -e "[$red FAIL $reset] $f >> $yellow Error: md5sum file not
found. $reset"
      continue # skip rest of loop
   fi

   if [ "$filehash" = "$gethash" ]
   then
       echo -e "[$green PASS $reset] $f"
   else
            echo -e "[$red FAIL $reset] $f >> $yellow Error: md5sum does not
match md5sum file. $reset"
      echo ""
      echo "FILE:`md5sum $f` ($f)"
      echo "HASH:`cat $f.md5` ($f.md5)"
      echo ""
   fi
done
echo ""
==============

You could modify these scripts to prompt for a search criteria rather than
have the value hard coded into the files, that minght be my next little
project, anyway, if you find them useful, enjoy!

Richard
Amateur Codemaster lol
2009/11/28 Richard Forth <richard.fo...@gmail.com>

> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Thanks Malc, I will try that on Monday.
>
> Malcolm Hunter wrote:
> >> As posted on Linux Format User Forums:
> >>
> >> Basically I was experimenting in shell script and was trying to create a
> >> useful script that took each file in the current directory (*.sh) and
> for
> >> each file, create a MD5 hash and save that hash in a file of the same
> name
> >> as the file (appended with a .md5 extension), so the basic script looked
> >> something like this:
> >>
> >>  *Code:*
> >> FILES="*.sh"
> >> for f in $FILES
> >> do
> >>    md5sum $f > $f.md5
> >> done
> >>
> >>
> >> Now, what is annoying is I have just run this at home on ubuntu (debian)
> >> and
> >> it has done the job as expected, however when I ran this on a test box
> at
> >> work today running CentOS (RedHat) it kept putting ALL the MD5 hashes in
> >> one
> >> file called "*.sh.md5", whereas what I was expecting, and what I got on
> >> Ubuntu, was say I had some files like so:
> >>
> >>  *Code:*
> >> shellscript1.sh
> >> shellscript2.sh
> >>
> >>
> >> after I run my script above in the current directory, and then run an
> "ls"
> >> I
> >> should get:
> >>
> >>  *Code:*
> >> shellscript1.sh
> >> shellscript1.sh.md5
> >> shellscript2.sh
> >> shellscript2.sh.md5
> >>
> >>
> >> Can anyone explain this deviant behaviour on Red Hat and give any tips
> >> that
> >> would allow the shellscript to work on either platform?
> >>
> >> Oh yes and if the code above is of any use you are welcome to it, and
> >> modify/redistribute as you see fit. It was a test really as I am
> learning
> >> "loops" at the moment and wondered if I could write a script that works
> >> with
> >> each file in a given folder.
> >
> > Using "*" gives unpredictable results depending on the context. Better to
> use the output of a command:
> >
> > LIST=`ls *.sh`
> >
> > Cheers,
> > Malc
> >
>
> - --
> *****
> Richard Forth
>
> Great Natural Health and wellbeing products
> http://www.aloevera-crowland.co.uk/
>
>
> ================================
> Do you want to work from home?
> Part-Time? Running your own business?
>
> Call me now to find out about opportunities
> in your area 07870 897755.
> ================================
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.9 (GNU/Linux)
> Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
>
> iEYEARECAAYFAksRppwACgkQQ6xgmxzOp8nVMwCZAclLYq/a7vRzDVgLuskPdoyj
> UXYAn2sdOBmtKBckkaKGbIv6ROOfBYHf
> =2mD7
> -----END PGP SIGNATURE-----
>



-- 
*****
Richard Forth

For great natural health, nutrition, animal care and beauty products, visit
my online store:
http://www.aloevera-crowland.co.uk

To change your life and circumstances:
http://www.soaringteam.com/users/invite/MyVideoPage.php?Passcode=2118&src=38&v=3

Text OPPORTUNITY and your name to:

07870 89 77 55
_______________________________________________
Peterboro mailing list
Peterboro@mailman.lug.org.uk
https://mailman.lug.org.uk/mailman/listinfo/peterboro

Reply via email to