Martin 的个人资料Martin's teeny tiny plac...照片日志列表更多 工具 帮助
2月27日

Guitar Hero casuality

Watch it, it could affect your day job..
 

Yikes!

2月1日

Little annoying things with AIX, ps and grep

Warning: if shell scripting and regex aren't your bag, stop reading ;)
 
Silly little bug I had to hunt down today while looking at an Oracle monitoring script someone wrote to use with Nagios..
 
Consider this output:
 
root@test(/tmp) # ps -ef | grep ora_pmon
  ora102  659494       1   0   Jan 26      -  1:32 ora_pmon_dev
  oracle 1011724       1   0   Oct 31      - 14:13 ora_pmon_deva
 
 
If you want to see if the db process is running, you just need to check for ora_pmon_<DBNAME>. Easy. But in this output, if I grep for 'ora_pmon_dev", I could get a hit even if only 'deva' is up and not 'dev'.. So you need to specify in the grep that you want the string to be at the END of the line, with no extras. So using a regular expression you'd use "ora_pmon_dev$".
 
Thing is, it didn't work!?
 
After half an hour of testing this, I discovered that 'ps' under AIX adds a trailing space to each line of output.
 
Easy - just adjust the regex to include the extra junk.. Again, it didn't work.. I discovered two things..
 
  1. "Good habits" sometimes bite you in the ass
  2. I miss perl :)

They're linked.. I tried to use "ora_pmon_dev\s*$" as a pattern. \s* to allow (optional) whitespace. It still didn't work.. Some more detective work and I found out that AIX's grep didn't understand \s at all. I guess I'm just too used to it in perl..! I usually get into the habit of using \s* to cover all whitespace cases, but here I guess testing for spaces-or-tabs will have to do:

grep 'ora_pmon_dev[    ]*$'

Where you type a space an a tab inside the brackets.. Makes it a lot harder to read and maintain afterwards, which I guess is exactly why \s exists in perl (and probably other regex engines).