scripting doubts

Cameron Simpson cs at zip.com.au
Thu Aug 6 07:05:16 UTC 2009


On 05Aug2009 09:52, Arthur Meeks Meeks <arthur.meeks.luppu at gmail.com> wrote:
| 2009/8/5 Cameron Simpson <cs at zip.com.au>
| > On 04Aug2009 20:04, Arthur Meeks Meeks <arthur.meeks.luppu at gmail.com>
| > wrote:
| > | What's the problem? When I can't log into a mysql database (mostly
| > | cause it is down) I got a "0", what I want is to discard these
| > | machines, so they're not included in /tmp/whatever.
| >
| > You're probably not getting to pay attention to the mysql exit status.
[...]
| >    mysql -h "$host" -ublah -pfhfhfhfhf -e "show grants..." >"$grants" \
| >    || { echo "skipping $host, maybe down" >&2
| >         continue
| >       }
| >    egrep ......... <"$grants" | wc -l ...
| 
| Hi Cameron,
| First off, thanks a lot.
| I've been trying to include those lines in my script without modifying it
| too much but it wasn't possible.
| How would you include your piece of code to this script:
| 
| #!/bin/bash
| PASS="fff"
| LIST="/tmp/perms"
| 
| for i in `cat /opt/lists/*clusters`; do echo -n $i: && mysql -h$i -uroot
| -p$PASS -e"show grants for 'user'@'10.10.%.%';" | egrep "REPLICATION
| CLIENT|ALL" | wc -l ; done > $LIST

Like this (untested):

for i in `cat /opt/lists/*clusters`
do
  mysql "-h$i" -uroot "-p$PASS" -e"show grants for 'user'@'10.10.%.%';" >mysql.out || continue
  echo -n $i:; egrep "REPLICATION CLIENT|ALL" <mysql.out | wc -l
done >"$LIST"

i.e. do the mysql command ad save the output.
If the mysql command failed (host down, for example), don't
bother with the second half of the loop.

Cheers,
-- 
Cameron Simpson <cs at zip.com.au> DoD#743
http://www.cskk.ezoshosting.com/cs/

C makes it easy for you to shoot yourself in the foot.  C++ makes that
harder, but when you do, it blows away your whole leg.
- Bjarne Stroustrup




More information about the users mailing list