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/trun k | 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|^FIL E|^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.
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/trun
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|^FIL
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
That did the trick. As I had said I only dabble in shell scripting occaisionly and I had forgotten about the eval command.
ASKER