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" ; with files you can do Code:
if [ -f "$filename" ] Code:
if [ ! -f "$filename" ] (So to speak add the ! to my if fi ;) ) Thx in advance ! |
Code:
if ! echo ${test} | grep -iq "abc" ; Code:
echo ${test} | grep -iq "abc" || echo "Not Found" |
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 |
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. |
I think that your sample is already a good answer:
Code:
if echo "$test" | grep -iq "abc" Code:
if echo "$test" | grep -iq "abc" Code:
if ! command 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" || { |
Quote:
|
It's best to avoid using names which are also commands, like "test".
|
You can also do this way:
Code:
if [[ ! "${test}" =~ .*((abc)|(ABC)).* ]]; then echo "Not found"; fi |
Quote:
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 |
Quote:
|
^ 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.) |
Quote:
As usual, practice matters ;) |
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 Code:
case $test in BTW this glob match is somewhat different from an RE! |
Regex
Code:
if [[ ! ${v1^^} =~ ABC ]] |
All times are GMT -5. The time now is 07:00 PM. |