[ILUG-BOM] Using grep 'AND'

Binand Sethumadhavan binand at gmail.com
Wed Feb 27 00:11:53 EST 2013


On 26 February 2013 23:45, gnulinuxist at gmail.com <gnulinuxist at gmail.com> wrote:
> I am using a script where I check if /home and /backup are present in /etc/mtab but can't manage the 'and' operator in grep. Currently I use the 'if grep -q "/home" ; then .........  then I do 'fi' and again 'if grep -q "/backup" to finally go to my commands. I tried to make grep do the AND combo but got errors as it works only on one line. I want my command to start only if both /home and /backup are present in mtab. With my dual check method it works, but I want a clean lean script.

How about:

if [ $( grep -cE '/(home|backup)' /etc/mtab ) = 2 ]; then
    echo Found both mounts
else
    echo Found only one of the two mounts
fi

(Refine as per your needs)

Binand


More information about the Linuxers mailing list