LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Echo with Grep Reverse ;) (https://www.linuxquestions.org/questions/linux-newbie-8/echo-with-grep-reverse-%3B-4175644330/)

iammike2 12-15-2018 03:07 AM

Echo with Grep Reverse ;)
 
Guys,

question please

I have this simple if fi (just hoping to expand my bash skills :study:)

Code:

    if echo ${test} | grep -iq "abc" ;
    then
    :
    else
    echo "Not Found"
    fi


with files you can do


Code:

if [ -f "$filename" ]
or if you want to check if it not exists (change the condition)

Code:

if [ ! -f "$filename" ]
So how do I do this with my 1st example ??

(So to speak add the ! to my if fi ;) )

Thx in advance !

ondoho 12-15-2018 03:15 AM

Code:

if ! echo ${test} | grep -iq "abc" ;
    then
    echo "Not Found"
    fi

or
Code:

echo ${test} | grep -iq "abc" || echo "Not Found"
PS: "$test" would be better than ${test}

iammike2 12-15-2018 03:19 AM

aaaaaaaahhhh thanks !

So the ! also works in this case (your example 1)??????? (wow stupid me, never thought of trying that)

Thanks lot, really appreciated

Ps: May I ask why $test is better then ${test} ? And what about in a Echo ?
For example

echo ${test} has been found or
echo $test has been found ?

Edit: I think I found it why that is. (Re: usage of {})

https://stackoverflow.com/questions/...en-and-in-bash

ondoho 12-15-2018 06:29 AM

tbh, i only tested the second option, but i have used constructs similar to the first. you might want to put the echo | grep in brackets, curly iirc.

make it a habit to (almost) always put variables in double quotes: "$test".
in your particular example, the curly brackets around test improve exactly nothing.

MadeInGermany 12-15-2018 06:57 AM

I think that your sample is already a good answer:
Code:

if echo "$test" | grep -iq "abc"
then
    :
else
    echo "Not Found"
fi

Using the else branch is a negation of
Code:

if echo "$test" | grep -iq "abc"
then
    echo "Found"
fi

The
Code:

if ! command
then

is ugly and not portable IMHO.
I would have liked a keyword unless in the meaning of if not (like perl has); strangly the shell has only the keyword until in the meaning of while not.

A portable alternative is command ||
Code:

echo "$test" | grep -iq "abc" || {
    echo "bad exit status"
}

If there is only one statement then the braces can be omitted.

ehartman 12-15-2018 07:02 AM

Quote:

Originally Posted by iammike2 (Post 5937380)
So how do I do this with my 1st example ??

(So to speak add the ! to my if fi ;) )

Look also at the -v option to grep, it reverses the grep condition.

smallpond 12-15-2018 07:47 AM

It's best to avoid using names which are also commands, like "test".

l0f4r0 12-15-2018 08:06 PM

You can also do this way:
Code:

if [[ ! "${test}" =~ .*((abc)|(ABC)).* ]]; then echo "Not found"; fi

iammike2 12-15-2018 09:04 PM

Quote:

Originally Posted by l0f4r0 (Post 5937648)
You can also do this way:
Code:

if [[ ! "${test}" =~ .*((abc)|(ABC)).* ]]; then echo "Not found"; fi


Wow ! I really have to look at this. Looks complicated


@smallpond. I used test because no other word could spring to mind, but next time I will use "avariablewithashortname" ;) But point well taken. Thx

Thx Guys for the answers, really helpful and good learning experience

l0f4r0 12-15-2018 09:31 PM

Quote:

Originally Posted by iammike2 (Post 5937666)
Wow ! I really have to look at this. Looks complicated

It's not. =~ operator allows to compare a variable to a regular expression ;)

ondoho 12-16-2018 01:40 AM

^ yes, but regular expressions definitely look complicated.
to my embarassment, i never managed to wrap my head around even the basics.

(i can deduce what this one means though.)

l0f4r0 12-16-2018 02:28 PM

Quote:

Originally Posted by ondoho (Post 5937696)
to my embarassment, i never managed to wrap my head around even the basics.

Here is a good tutorial for starters: http://tldp.org/LDP/abs/html/x17129.html
As usual, practice matters ;)

MadeInGermany 12-16-2018 03:37 PM

The =~ is an ERE (like egrep).
All RE are fuzzy so you never need .* at the beginning or end!
And the [[ ]] does word splitting BEFORE variable substitution, and NO globbing, so $var does not need quotes. (In contast to the [ ].)
Code:

if [[ ! $test =~ (abc)|(ABC) ]]; then echo "Not found"; fi
Last but not least, the traditional (and portable) check is with a case-esac
Code:

case $test in
*abc*|*ABC*)
;;
*)
  echo "Not found"
;;
esac

You see the empty action on the positive catch, and the * (=catch-all) works like an else branch.
BTW this glob match is somewhat different from an RE!

chrism01 12-18-2018 10:21 PM

Regex
Code:

if [[ ! ${v1^^} =~ ABC ]]
then
    echo "no match"
fi



All times are GMT -5. The time now is 07:00 PM.