Jump to content

Problem - Grep cammand in shell script

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1
prasannasupp

prasannasupp

    Newbie

  • Members
  • Pip
  • 1 posts
Hi,

I have written the following shell script -

Error_String="error"
var1="| grep -v note1 | grep -v note2"
grep -i $Error_String /users/mqm/Pwork/Err/*.out $var1


The above script gives error saying "grep: can't open |
grep: can't open grep
grep: can't open -v" etc

In my program note1 and note2 strings are dynamically generated, so I cannot hardcode those in grep command.

Could anyone please help me to resolve this problem

Thanks in advance,
Prasanna

#2
asafe

asafe

    Programmer

  • Members
  • PipPipPipPip
  • 107 posts
First of all, say excatily you are trying to acomplish.
var1 is supposed to contain the string "| grep -v note1 | grep -v note2" or the result of grep command?

#3
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,721 posts
You need to change note1 and note2 to $note1 and $note2. I would do it like this:

result=$(grep -i $Error_String /users/mqm/Pwork/Err/*.out | grep -v $note1 | grep -v $note2)

The reason why $var1 didn't work was because the shell automatically quoted it when it expanded the variable. So the command line actually looked like this:

grep -i $Error_String /users/mqm/Pwork/Err/*.out "| grep -v note1 | grep -v note2"

In addition to grepping every .out file, grep would have also tried to open a file called | grep -v note1 | grep -v note2, which of course doesn't exist.
sudo rm -rf /