Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 27 Jun 2012 10:25:46 -0400
From:      Aleksandr Miroslav <alexmiroslav@gmail.com>
To:        freebsd-questions@freebsd.org
Subject:   shell scripting: grepping multiple patterns, logically ANDed
Message-ID:  <CACcSE1zwRcU_VQ16A2wiG4yWk4RBRykTXBono3xspw97zhDs7w@mail.gmail.com>

next in thread | raw e-mail | index | archive | help
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.



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?CACcSE1zwRcU_VQ16A2wiG4yWk4RBRykTXBono3xspw97zhDs7w>