August
16

The integrators at work had a problem recently with a shell script that generates SQL deployment and rollback scripts on the fly based on SQL patches submitted by developers. The problem was that sometimes the name of an object would include a “(” at the end of it (e.g. “FN_RESET(” instead of “FN_RESET“). Here’s a quick solution I whipped up for them:

#if $TEST_END doesn't become an empty string,
#then OBJECT_NAME ends with "(" so we strip it
TEST_END=$(echo "$OBJ_NAME" | grep \($)
[ -z "$TEST_END" ] || OBJ_NAME=$(echo ${OBJ_NAME%\(})
echo “OBJ_NAME is : $OBJ_NAME”

The sharp eye amongst you will notice that this works for FOO( but not for FOO(NANY. If you want to strip everything after the ( and including the ( the you want this:

TEST_END=$(echo "$OBJ_NAME" | grep \()
[ -z "$TEST_END" ] || OBJ_NAME=$(echo “$OBJ_NAME” | awk ‘{split($0,a,”(”);print a[1]}’)
echo “OBJ_NAME is : $OBJ_NAME”

Notice how the grep no longer uses the $ anchor to match a ( at the end of the line? Now if $TEST_END isn’t empty, then we have a ( in our string, so we split on that and return everything to the left (first element in the a[]).

0