Closed Thread
Results 1 to 3 of 3

Thread: Problem - Grep cammand in shell script

  1. #1
    prasannasupp is offline Newbie
    Join Date
    Sep 2009
    Posts
    1
    Rep Power
    0

    Problem - Grep cammand in shell script

    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. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    asafe's Avatar
    asafe is offline Programmer
    Join Date
    Jul 2009
    Location
    Here
    Posts
    110
    Rep Power
    10

    Re: Problem - Grep cammand in shell script

    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?

  4. #3
    Join Date
    Oct 2007
    Location
    /dev/null
    Posts
    4,513
    Blog Entries
    8
    Rep Power
    59

    Re: Problem - Grep cammand in shell script

    You need to change note1 and note2 to $note1 and $note2. I would do it like this:

    Code:
    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 /

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 2 users browsing this thread. (0 members and 2 guests)

Similar Threads

  1. [HELP] convert Perl script to Bash shell script
    By Egypte in forum Linux Programming and Scripting
    Replies: 2
    Last Post: 04-24-2011, 05:37 PM
  2. A Shell script problem
    By veda87 in forum Linux Programming and Scripting
    Replies: 2
    Last Post: 08-18-2010, 07:51 PM
  3. Shell Script Help!
    By SeanStar in forum Linux Programming and Scripting
    Replies: 6
    Last Post: 08-12-2010, 05:20 PM
  4. Shell script string operator problem
    By DarkLordoftheMonkeys in forum Linux Programming and Scripting
    Replies: 2
    Last Post: 11-12-2009, 01:32 PM
  5. Grep Problem
    By dargueta in forum Linux Programming and Scripting
    Replies: 5
    Last Post: 10-22-2009, 01:03 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts