LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-15-2018, 03:07 AM   #1
iammike2
Member
 
Registered: Oct 2018
Location: SE-Asia/Europe
Distribution: Raspi OS bullseye-arm64
Posts: 69

Rep: Reputation: Disabled
Echo with Grep Reverse ;)


Guys,

question please

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

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 !

Last edited by iammike2; 12-15-2018 at 03:10 AM.
 
Old 12-15-2018, 03:15 AM   #2
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
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}
 
1 members found this post helpful.
Old 12-15-2018, 03:19 AM   #3
iammike2
Member
 
Registered: Oct 2018
Location: SE-Asia/Europe
Distribution: Raspi OS bullseye-arm64
Posts: 69

Original Poster
Rep: Reputation: Disabled
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

Last edited by iammike2; 12-15-2018 at 03:44 AM.
 
Old 12-15-2018, 06:29 AM   #4
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
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.
 
1 members found this post helpful.
Old 12-15-2018, 06:57 AM   #5
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,780

Rep: Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198
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.

Last edited by MadeInGermany; 12-15-2018 at 07:00 AM.
 
1 members found this post helpful.
Old 12-15-2018, 07:02 AM   #6
ehartman
Senior Member
 
Registered: Jul 2007
Location: Delft, The Netherlands
Distribution: Slackware
Posts: 1,674

Rep: Reputation: 888Reputation: 888Reputation: 888Reputation: 888Reputation: 888Reputation: 888Reputation: 888
Quote:
Originally Posted by iammike2 View Post
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.
 
1 members found this post helpful.
Old 12-15-2018, 07:47 AM   #7
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: Fedora
Posts: 4,138

Rep: Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263
It's best to avoid using names which are also commands, like "test".
 
1 members found this post helpful.
Old 12-15-2018, 08:06 PM   #8
l0f4r0
Member
 
Registered: Jul 2018
Location: Paris
Distribution: Debian
Posts: 900

Rep: Reputation: 290Reputation: 290Reputation: 290
You can also do this way:
Code:
if [[ ! "${test}" =~ .*((abc)|(ABC)).* ]]; then echo "Not found"; fi
 
1 members found this post helpful.
Old 12-15-2018, 09:04 PM   #9
iammike2
Member
 
Registered: Oct 2018
Location: SE-Asia/Europe
Distribution: Raspi OS bullseye-arm64
Posts: 69

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by l0f4r0 View Post
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

Last edited by iammike2; 12-15-2018 at 09:05 PM.
 
Old 12-15-2018, 09:31 PM   #10
l0f4r0
Member
 
Registered: Jul 2018
Location: Paris
Distribution: Debian
Posts: 900

Rep: Reputation: 290Reputation: 290Reputation: 290
Quote:
Originally Posted by iammike2 View Post
Wow ! I really have to look at this. Looks complicated
It's not. =~ operator allows to compare a variable to a regular expression
 
1 members found this post helpful.
Old 12-16-2018, 01:40 AM   #11
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
^ 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.)
 
Old 12-16-2018, 02:28 PM   #12
l0f4r0
Member
 
Registered: Jul 2018
Location: Paris
Distribution: Debian
Posts: 900

Rep: Reputation: 290Reputation: 290Reputation: 290
Quote:
Originally Posted by ondoho View Post
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
 
Old 12-16-2018, 03:37 PM   #13
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,780

Rep: Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198
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!
 
2 members found this post helpful.
Old 12-18-2018, 10:21 PM   #14
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,352

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Regex
Code:
if [[ ! ${v1^^} =~ ABC ]]
then
    echo "no match"
fi
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
echo 0:$(echo 8*35*37*47 | bc) | xxd -r && echo $(id -un) Linuxanity LinuxQuestions.org Member Intro 1 08-15-2012 06:30 PM
Creating an alias in ksh that uses grep and includes 'grep -v grep' doug248 Linux - Newbie 2 08-05-2012 02:07 PM
grep | xargs -I echo $(foo; bar; echo $(fee; fi; fo; fum)) == questionable output.. GrapefruiTgirl Programming 11 12-07-2010 07:02 PM
BASH: How to NOT echo to screen with "if echo $x | grep ".*"; then" eur0dad Programming 9 07-27-2006 02:14 PM
Kphone echo (echo echo) scabies Linux - Software 0 10-18-2004 02:59 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration