Posted inInformation Technology

Substring Exists in String

pessoa programando hacker

Today I got to burn about three hours looking online for an answer to what F$LOCATE() makes 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.

How you do it on OpenVMS

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.

Linux Hell

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.

Official documentation

That’s just lovely isn’t it. Not one single mention of the words “locate”, “contains” or “exists.” Nothing as straight forward as F$LOCATE.

Roland Hughes started his IT career in the early 1980s. He quickly became a consultant and president of Logikal Solutions, a software consulting firm specializing in OpenVMS application and C++/Qt touchscreen/embedded Linux development. Early in his career he became involved in what is now called cross platform development. Given the dearth of useful books on the subject he ventured into the world of professional author in 1995 writing the first of the "Zinc It!" book series for John Gordon Burke Publisher, Inc.

A decade later he released a massive (nearly 800 pages) tome "The Minimum You Need to Know to Be an OpenVMS Application Developer" which tried to encapsulate the essential skills gained over what was nearly a 20 year career at that point. From there "The Minimum You Need to Know" book series was born.

Three years later he wrote his first novel "Infinite Exposure" which got much notice from people involved in the banking and financial security worlds. Some of the attacks predicted in that book have since come to pass. While it was not originally intended to be a trilogy, it became the first book of "The Earth That Was" trilogy:
Infinite Exposure
Lesedi - The Greatest Lie Ever Told
John Smith - Last Known Survivor of the Microsoft Wars

When he is not consulting Roland Hughes posts about technology and sometimes politics on his blog. He also has regularly scheduled Sunday posts appearing on the Interesting Authors blog.