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
Official documentationnocasematch
(see the description ofshopt
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 variableBASH_REMATCH
. The element ofBASH_REMATCH
with index 0 is the portion of the string matching the entire regular expression. The element ofBASH_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.” Nothing as straight forward as F$LOCATE.