Link to home
Start Free TrialLog in
Avatar of swymer
swymer

asked on

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.
Avatar of swymer
swymer

ASKER

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.
ASKER CERTIFIED SOLUTION
Avatar of noci
noci

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of swymer

ASKER

Thanks,

That did the trick.  As I had said I only dabble in shell scripting occaisionly and I had forgotten about the eval command.