From owner-freebsd-questions@FreeBSD.ORG Wed Jun 27 15:40:05 2012 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 86A61106566B for ; Wed, 27 Jun 2012 15:40:05 +0000 (UTC) (envelope-from tundra@tundraware.com) Received: from ozzie.tundraware.com (ozzie.tundraware.com [75.145.138.73]) by mx1.freebsd.org (Postfix) with ESMTP id 30CBF8FC12 for ; Wed, 27 Jun 2012 15:40:05 +0000 (UTC) Received: from [10.219.131.188] ([66.175.245.1]) (authenticated bits=0) by ozzie.tundraware.com (8.14.5/8.14.5) with ESMTP id q5RFPJC6091035 (version=TLSv1/SSLv3 cipher=DHE-RSA-CAMELLIA256-SHA bits=256 verify=NO); Wed, 27 Jun 2012 10:25:20 -0500 (CDT) (envelope-from tundra@tundraware.com) Message-ID: <4FEB25E8.6000701@tundraware.com> Date: Wed, 27 Jun 2012 10:25:28 -0500 From: Tim Daneliuk Organization: TundraWare Inc. User-Agent: Mozilla/5.0 (X11; Linux i686; rv:13.0) Gecko/20120615 Thunderbird/13.0.1 MIME-Version: 1.0 To: Aleksandr Miroslav References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Greylist: Sender succeeded SMTP AUTH, not delayed by milter-greylist-4.2.7 (ozzie.tundraware.com [75.145.138.73]); Wed, 27 Jun 2012 10:25:20 -0500 (CDT) X-TundraWare-MailScanner-Information: Please contact the ISP for more information X-TundraWare-MailScanner-ID: q5RFPJC6091035 X-TundraWare-MailScanner: Found to be clean X-TundraWare-MailScanner-From: tundra@tundraware.com X-Spam-Status: No Cc: freebsd-questions@freebsd.org Subject: Re: shell scripting: grepping multiple patterns, logically ANDed X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list Reply-To: tundra@tundraware.com List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 27 Jun 2012 15:40:05 -0000 On 06/27/2012 09:25 AM, Aleksandr Miroslav wrote: > hello, > > I'm not sure if this is the right forum for this question, but here > goes. > > I have the following in a shell script: > > > #!/bin/sh > > if [ "$#" -eq "0" ]; then > find /foo > fi > if [ "$#" -eq "1" ]; then > find /foo | grep -i $1 > fi > if [ "$#" -eq "2" ]; then > find /foo | grep -i $1 | grep -i $2 > fi > if [ "$#" -eq "3" ]; then > find /foo | grep -i $1 | grep -i $2 | grep -i $3 > fi > > Is there an easier/shorter way to do this? If there are 15 arguments > supplied on the command line, I don't necessarily want to build 15 if > statements. > > Thanks in advance for your answers. The following solution relies on the fact that you can include multiple patterns for grep to match with the '-e' argument: #!/bin/sh PATTERNS=`echo " $*" | sed s/\ /\ -e\ /g` find /foo | grep $PATTERNS Notice that when constructing the $PATTERNS string out of the command line args, you have to quote them with a prepended space character. That's because the subsequent 'sed' substitution needs to find a space *before* each argument which it then replaces with "-e ". ----------------------------------------------------------------------- Tim Daneliuk