Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

for loop on pair

0 views
Skip to first unread message

mathieu

unread,
Nov 24, 2009, 8:57:32 AM11/24/09
to
I'd like to be able to iterate over pairs. Something like (pseudo
shell script)

for i,j in file1,md51 \
file2,md52 \
file3,md53
do
compare(md5sum file $i, $j)
done

Thanks

Andrew McDermott

unread,
Nov 24, 2009, 9:11:57 AM11/24/09
to
mathieu wrote:

for pair in file1,md51 \
file2,md52 \
file3,md53
do
i=${pair%,*}
j=${pair#*,}
compare(md5sum file $i, $j
done


Andrew

Rakesh Sharma

unread,
Nov 24, 2009, 3:38:46 PM11/24/09
to

for var in file1,md51 \
file2,md52 \
file3,md53
do
IFS=','; set -f; set x $var; shift
compare(md5sum file "$1", "$2")
done

Janis Papanagnou

unread,
Nov 24, 2009, 5:49:08 PM11/24/09
to

Here's a code frame...

while read -r md5 f
do
set -- $( md5sum "$f" )
case $1 in
$md5)
printf "Match: md5('%s') is '%s'\n" \
"$f" "$md5" ;;
*) printf "Mismatch: md5('%s') is '%s' but expected '%s'\n" \
"$f" "$md5" "$1" ;;
esac
done

And depending how you want to provide the data pairs...

1) a process generates the md5sum/file pairs

md5sum *.txt *.pdf |
while
# while loop as above
done

2) reading the md5sum/file pairs list from a file

while
# while loop as above
done < file_with_pairs

3) hard coded md5sum/file pairs within the script

while
# while loop as above
done << EOT
a8d67af107b327e1e47795b52762ad18 hello_world.c
190932301eefaae68607941f33ac474c hi_there.txt
EOT


(The usual caveats apply.)

Janis

>
> Thanks

0 new messages