#!\bin\sh
TEST=test.log
GREP=\usr\bin\grep
$GREP -i 'dog\|cat' ${TEST}
Why doesn't grep run at all?
#!\bin\sh
TEST=test.log
GREP=\usr\bin\grep
$GREP -i 'dog\|cat' ${TEST}
Why doesn't grep run at all?
Try:
eval $GREP ...
For indirect references you have to use eval.
HTH Chris
In line 1 and 3 all backslashes should lean the other way...
What about:
#!/bin/sh
TEST=test.log
GREP=/usr/bin/egrep
$GREP -i 'dog|cat' ${TEST}
maybe should you
eval "$GREP -i 'dog|cat' ${TEST}"
if /usr/bin is your $PATH then you could
GREP=egrep
too !!
Hi guys! Thanks for your help! I've made the following changes..funny thing is that grep still doesn't run
#!/bin/sh
TEST=test.log
GREP=/usr/bin/grep
eval "$GREP -i 'dog\|cat' ${TEST}"
And why must I use egrep when grep should work in this case.
What does that mean in concrete terms? Is there no output?
Did you try to run that command in the shell and see if it will work?
/usr/bin/grep -i 'dog\|cat' test.log
Also try
/usr/bin/grep -iE 'dog\|cat' test.log
You don't need to use eval - or egrep if you escape the infix |
Of course it runs... maybe it just doesn't find anything?
Show some of your input file
$ echo cats and dogs > file1
$ GREP=$(which grep)
$ $GREP "cats\|dogs" file1
cats and dogs
grep -i 'dog\|cat' test.log
works in the shell! but /usr/bin/grep -i 'dog\|cat' test.log doesn't
---------- Post updated at 11:03 PM ---------- Previous update was at 10:59 PM ----------
Test file for grep input
this is a cat.
this is a dog.
this is a fox.
this is a bird.
this is a frog.
And cat and dogs are Man's best friends.
I made the following changes and the script now runs fine:
#!/bin/sh
TEST=animal.log
eval "grep -i 'dog\|cat' ${TEST}"
output:
this is a cat.
this is a dog.
And cat and dogs are Man's best friends.
i then do a whereis grep:
grep: /usr/bin/grep /usr/local/bin/grep /usr/man/man1/grep.1
I really don't understand why
#!/bin/sh
TEST=test.log
GREP=/usr/bin/grep
eval "$GREP -i 'dog\|cat' ${TEST}"
doesn't work
Standard grep uses POSIX BRE's (Basic regular expressions) which do not include alternation. So one should use egrep for that which supports ERE's (Extended Regular Expressions). The reason that \| works with grep for some is that GNU grep added this as an extension to BRE, but this is not standard.
thanks you for the detailed explanation about "\|", after using egrep instead, everything works out fine ![]()