From owner-freebsd-questions Sat Nov 21 04:51:22 1998 Return-Path: Received: (from majordom@localhost) by hub.freebsd.org (8.8.8/8.8.8) id EAA10275 for freebsd-questions-outgoing; Sat, 21 Nov 1998 04:51:22 -0800 (PST) (envelope-from owner-freebsd-questions@FreeBSD.ORG) Received: from awfulhak.org (awfulhak.force9.co.uk [195.166.136.63]) by hub.freebsd.org (8.8.8/8.8.8) with ESMTP id EAA10254 for ; Sat, 21 Nov 1998 04:51:16 -0800 (PST) (envelope-from brian@Awfulhak.org) Received: from woof.lan.awfulhak.org (woof.lan.Awfulhak.org [172.16.0.7]) by awfulhak.org (8.8.8/8.8.8) with ESMTP id MAA17903; Sat, 21 Nov 1998 12:50:45 GMT (envelope-from brian@Awfulhak.org) Received: from woof.lan.awfulhak.org (localhost [127.0.0.1]) by woof.lan.awfulhak.org (8.9.1/8.9.1) with ESMTP id MAA00593; Sat, 21 Nov 1998 12:32:47 GMT (envelope-from brian@woof.lan.awfulhak.org) Message-Id: <199811211232.MAA00593@woof.lan.awfulhak.org> X-Mailer: exmh version 2.0.2 2/24/98 To: Glen Mann cc: freebsd-questions@FreeBSD.ORG Subject: Re: sh programming question In-reply-to: Your message of "Wed, 18 Nov 1998 13:48:08 EST." <36531668.2C2CD440@itw.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Date: Sat, 21 Nov 1998 12:32:46 +0000 From: Brian Somers Sender: owner-freebsd-questions@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG > 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 , , 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