| S | M | T | W | T | F | S |
|---|---|---|---|---|---|---|
| « Sep | Nov » | |||||
| 1 | 2 | 3 | 4 | |||
| 5 | 6 | 7 | 8 | 9 | 10 | 11 |
| 12 | 13 | 14 | 15 | 16 | 17 | 18 |
| 19 | 20 | 21 | 22 | 23 | 24 | 25 |
| 26 | 27 | 28 | 29 | 30 | 31 | |
- Information Technology (77)
- Thankyou Sir May I have Another (18)
- Uncategorized (39)
- February 26, 2010: How We Sissify the World
- February 17, 2010: Funding al-Qaeda With Taxpayer Dollars
- February 17, 2010: The New Definition of Googling
- February 12, 2010: Why You Suck as a Technical Recruiter
- January 25, 2010: Only We Can Fix This
- January 20, 2010: Y2K Phase Two
- January 15, 2010: The Rest of the W-2 Story
- January 11, 2010: The Doctors Without Limits
- January 7, 2010: For Whom the Hard Drive Tolls
- December 23, 2009: Authonomy.com
- February 2010
- January 2010
- December 2009
- November 2009
- October 2009
- September 2009
- August 2009
- July 2009
- June 2009
- May 2009
- April 2009
- March 2009
- February 2009
- January 2009
- December 2008
- November 2008
- October 2008
- September 2008
- August 2008
- July 2008
- June 2008
- April 2008
- March 2008
- January 2008
- December 2007
- November 2007
- October 2007
- September 2007
- August 2007
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”
October 8, 2008 at 6:37 am
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.