Substring Exists in String

Today I got to burn about three hours looking online for an answer to a simple task. What simple task you ask? Why, I was trying to verify one string existed inside of another string while working in Bash under Linux. You see, I learned to write software on robust systems. Yes, I know, the Linux/Unix developer motto is “test nothing, just let it fail”, but I was taught to write good software. Simply posting a blurb about prerequisites in an obscure document file and berating someone who couldn’t find that doc for not being skilled simply isn’t in my fiber…well, there might be a chosen few I would do that to, but they deserve it for many other reasons, I certainly wouldn’t do it to the general public.

In the world of OpenVMS you would simply use a lexical function F$LOCATE() and it would look much like the following

$ l_x = f$locate( “ENDXXX”, “”line_in_str’”)

The single ticks around the line_in_str variable had to be there to force DCL to use the value of the variable rather than the name of the variable in the function call.

After three hours of searching using every variation of “locate” “contains” “find” in the Yahoo, Google, and Ask search engines, I finally found ONE document which bothered to give a useful example. Every other method I found involved calling sed or awk with a regular expression which looked like the output from a desktop calculator which was being profusely beaten by a Ballpean hammer.

What was I trying to do? I was trying to verify that SOME postgres directory existed in the current definition of PATH before continuing on. If it didn’t exist I wanted to force a definition in. Ultimately I ended up with the following snippet of code as the fruits of three hours.

##
## Now we need to check for postgres in the path
##
pp=$PATH

if !( [[ “$pp” =~ “postgres” ]] ); then
PATH=/usr/lib/postgresql/8.3/bin:$PATH
export PATH
echo “PATH updated to include postgres”
fi;

Once again, Unix/Linux documentation has proven to be expert friendly. I defy you to get the above mentioned search engines to let you find =~ in a search string. Here is what the GNU version of the Bash documentation had to say:

An additional binary operator, ‘=~’, is available, with the same precedence as ‘==’ and ‘!=’. When it is used, the string to the right of the operator is considered an extended regular expression and matched accordingly (as in regex3)). The return value is 0 if the string matches the pattern, and 1 otherwise. If the regular expression is syntactically incorrect, the conditional expression’s return value is 2. If the shell option nocasematch (see the description of shopt in Bash Builtins) is enabled, the match is performed without regard to the case of alphabetic characters. Substrings matched by parenthesized subexpressions within the regular expression are saved in the array variable BASH_REMATCH. The element of BASH_REMATCH with index 0 is the portion of the string matching the entire regular expression. The element of BASH_REMATCH with index n is the portion of the string matching the nth parenthesized subexpression.

That’s just lovely isn’t it. Not one single mention of the words “locate”, “contains” or “exists”

One Response to “Substring Exists in String”

  1. neilrieck says:

    I have always wondered if software like: UNIX, LINUX, C, C++, C# etc. were nothing more than job protection. Imagine trying to understand the MAN pages if they were written in an language other than English. I’m waiting for the next big thing to be documented in Russian, Indian, or Chinese but not English.

Leave a Reply