Date: Sat, 21 Nov 1998 12:32:46 +0000 From: Brian Somers <brian@Awfulhak.org> To: Glen Mann <gmann@itw.com> Cc: freebsd-questions@FreeBSD.ORG Subject: Re: sh programming question Message-ID: <199811211232.MAA00593@woof.lan.awfulhak.org> In-Reply-To: Your message of "Wed, 18 Nov 1998 13:48:08 EST." <36531668.2C2CD440@itw.com>
next in thread | previous in thread | raw e-mail | index | archive | help
> Sorry if this should go elsewhere, but it should be pretty simple. > > In a Bourne shell script, I want to compare two lists, obtained e.g. > with > list1=`ls $dirname1` > list2=`ls $dirname2` > in order to determine what elements are in list1 and list2, or in one > but not the other. The two lists will always be sorted, but not > necessarily directory listings. I'd like to do this without using > temporary files. I could use arrays in a Bash script and step through > the lists, but want to stick with sh. Is there an easy way to do this, > perhaps using sort and uniq? You best bet is probably something like: common() { ans= srch=" $2 " for f in $1 do test ."${srch% $f *}" != ."$srch" && echo $f done unset srch } calling ``common "$list1" "$list2"''. Just change the && to || for a notcommon function. Although kludgy, it's 100% builtin and therefore relatively fast. > Thanks > -Glen -- Brian <brian@Awfulhak.org>, <brian@FreeBSD.org>, <brian@OpenBSD.org> <http://www.Awfulhak.org> Don't _EVER_ lose your sense of humour.... To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-questions" in the body of the message
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?199811211232.MAA00593>