bash scripting - Using pipe character in grep in scripts

Hello,

I am trying to write a little shell wrapper script for a few commonly used SVN commands.  The problem is that I have a regex in my final executed command that contains the pipe or "|" character as an OR in my regex.  I have tried MANY forms of quoting, backslashign, etc to get this to parse properly when run but the best I have gotten so far is shown below and when it is run I get an error because the shell tries to recognize the "|" in the regex as a further pipe.  Im sure that the solution is simple but My best attempts as hacking and searching for a solution have been for naught.  The command I am trying to get the script to run in the end is something like:

svn log file:///var/svn/repos/trunk | grep -P '^r\d+\s|^FILE|^MERGE|^-------'

which runs fine at the command line.
================= Begin Inserted Code =================
#! /bin/sh

CMDROOT=$1
shift;

bindir='/usr/bin'
cmd="${bindir}/svn $CMDROOT"

help="\
mysvn is a wrapper which pipes some SVN commands through grep to simplify the output.

Usage:
  mysvn log [-D][-r VALID_REVISION_STRING] VALID_REPOSITORY_PATH
  mysvn diff [-D][-r VALID_REVISION_STRING] VALID_REPOSITORY_PATH
 
Common options:
  -D   Show More Detail
  -r   Specify which Revision(s) to examine, defaults to HEAD
"
if [ $CMDROOT = 'log' ]; then
      grepString="grep -P '^r\\d+\\s|^FILE|^MERGE|^-------'"
else
      grepString="grep '^Index:'"
fi

while [ "$#" -gt 1 ]
do
    case "$1" in
      -h|--help|-\?)
         echo "$help"
         exit 0
         ;;
      -D|-d)
            if [ $CMDROOT = 'log' ]; then
                  grepString="grep -P '^r\\d+\\s|^DEVELOPER|^FILE|^MERGE|^-------'"
            else
                  grepString="grep -P '^Index:|^@@'"
            fi
          ;;
      -r)
            shift
            revisionString="-r $1"
            ;;
      -*)
         echo "mysvn: invalid option: $1" 1>&2
         echo "$advice" 1>&2
         exit 1
         ;;
    esac
    shift
done

finalCmd="$cmd $revisionString file:///var/svn/repos/$1 | $grepString"
echo $finalCmd

#`$finalCmd`
`$cmd $revisionString file:///var/svn/repos/$1 | $grepString`

exit 0
================= End Inserted Code =================

P.S. Any helps or suggestions on the script would be greatly appreciated.  I am only a very part-time UNIX scripter.
swymerAsked:
Who is Participating?
 
nociSoftware EngineerCommented:
Have you allready tried

eval $finalCmd

this is because you need to reparse the contents of that string as a command, otherwise the variable is expected to be the command end the |
as part of it... If you still have problems a single \ should be before every | within the grep expression.

this works (giving one line with TEST on it).
 
$ echo -e 'test\nTEST\ntest' | egrep '^TEST|^FAILED'
TEST
$ x="echo -e 'test\nTEST\ntest' | egrep '^TEST|^FAILED'"
$ echo $x
echo -e 'test\nTEST\ntest' | egrep '^TEST|^FAILED'
$ $x
'test
TEST
test' | egrep '^TEST|^FAILED'
$ eval $x
TEST
$ (exec $x)
'test
TEST
test' | egrep '^TEST|^FAILED'

(this was done on linux with a bash shell & gnu grep)
0
 
swymerAuthor Commented:
Ooops sorry, I just realized that I left a little debugging in the very end of the file, that shows some of how I have tried to run things.  The echo was just for debugging and I had tried to just run `$finalCmd` but that was not very successful so I was trying to exec the contents of $finalCmd directly and this is where I left off.
0
 
swymerAuthor Commented:
Thanks,

That did the trick.  As I had said I only dabble in shell scripting occaisionly and I had forgotten about the eval command.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.