Now for searching for a particular string, previously I chose sed, because I was replacing macros. But now, I just want to test for existence - does this file contain this string - for which sed seemed wrong. So I chose grep, a somewhat obvious option. I made the changes, tested it out by running the shell script directly, and things were cool. Enter Ant.
For the build proper, we use Ant. In this, we use the Execute plugin to run that shell script I just mentioned. Now this is where my problem started.
What I saw:
execute:
[exec] Result: 1
[exec] Result: 1
What I expected:
execute:
[exec] Found Macro, replacing it.
[exec] Done
[exec] Result: 1
[exec] Found Macro, replacing it.
[exec] Done
[exec] Result: 1
Obviously something is amiss. "Done" should be printed no matter what, unless there is an error. But there was no error being printed. So I hunted down the problem line:
gn=$(grep "['\"]{[[:alpha:]_][[:alnum:]_]*}" notice_tmp.js)
And I narrowed that down to just grep. I was seeing this strange behavior where as I changed the search string, sometimes the execution would finish and print "Done", and sometimes it didn't. I was fairly perplexed till I finally said the problem out loud to myself: "Sometimes it exits correctly, sometimes it doesn't". . . ... 'exit'! Eureka!
Ok so maybe people who deal with shell scripts a lot are like "no shit Sherlock" but I'm not, so it took me a while, especially with the problem being a few layers deep in commands. Anyways, it occurred to me that a call to exit inside grep must be bubbling up to Ant causing it to exit early. So when there IS a match found by grep, it doesn't exit and continues in my script, but when there ISN'T a match, it exits immediately and my script is just out of luck.
Fortunately, Google solved this for me rather quickly. Since an exit is bubbling up, just put another command on top of it to catch it:
gn=$(grep "['\"]{[[:alpha:]_][[:alnum:]_]*}" notice_tmp.js | echo)
or, if that doesn't work out for you, another way:
And there we go, echo catches the exit call, and my script keeps on running