Ok I got some help over at http://linuxquestions.org. But I'm stopped again.
Now on to the next weird issue. I cleaned up the script a little and *should* be getting the output I expect. In fact it echos to the screen just fine. But doesn't seem to get expanded into a command that works. But, the command that gets echo'd to the screen runs just fine by itself.
Consider the following output where I copy/paste the echo'd command.
[[START DATABASE]
Database: test
people t2
people
/usr/bin/mysql -uroot test -Bse 'optimize TABLE people;'
test.$TABLENAME optimize error Table 'test.$TABLENAME' doesn't exist
t2
/usr/bin/mysql -uroot test -Bse 'optimize TABLE t2;'
test.$TABLENAME optimize error Table 'test.$TABLENAME' doesn't exist
[END DATABASE]
[br8kwall@localhost ~]$ /usr/bin/mysql -uroot test -Bse 'optimize TABLE t2;'
test.t2 optimize status OK
[br8kwall@localhost ~]$
and here is the modified script Code: #!/bin/sh
MUSER="root"
MPASS=""
MHOST="localhost"
MYSQL="$(which mysql)"
MYSQLDUMP="$(which mysqldump)"
# the Bs makes the output appear without the formatting
# and header row.
# Step 1: list all databases
DBS="$($MYSQL -u$MUSER -Bse 'show databases')"
for db in ${DBS[@]}
do
# Step 2: list all tables in the databases
echo "$MYSQL -u$MUSER $db -Bse 'show tables'"
TABLENAMES="$($MYSQL -u$MUSER $db -Bse 'show tables')"
echo "[START DATABASE]"
echo "Database: "$db
echo ${TABLENAMES[@]}
# Step 3: perform an optimize (or other op) for all tables returned
for TABLENAME in ${TABLENAMES[@]}
do
echo $TABLENAME
echo "$MYSQL -u$MUSER $db -Bse 'optimize TABLE $TABLENAME;'"
$MYSQL -u$MUSER $db -Bse 'optimize TABLE $TABLENAME;'
done
echo "[END DATABASE]"
done
|