Hi,
I'm trying to apply some mysql .sql dump files to multiple databases, and would like to use a ternary operator to modify the name of the database the dump is being read back into.
Details:
Say I have the following files:
Code:
123_01_DBNAMEM_tableName.sql
234_01_DBNAMEC_tableName.sql
234_02_DBNAMER_tableName.sql
432_01_DBNAMEM_tableName.sql
456_01_DBNAMEM_tableName.sql
543_01_DBNAMEC_tableName.sql
543_02_DBNAMEM_tableName.sql
543_03_DBNAMER_tableName.sql
567_01_DBNAMEM_tableName.sql
678_01_DBNAMEC_tableName.sql
And I have the following databases:
Code:
DBNAMEC-realm1
DBNAMEC-realm2
DBNAMEC-realm3
DBNAMEM-realm1
DBNAMEM-realm2
DBNAMEM-realm3
DBNAMER
So I want anything with DBNAMEC in its name to be applied to each of DBNAMEC-realm[1-3] and, anything with DBNAMEM in its name to be applied to each of DBNAMEM-realm[1-3] but files containing DBNAMER in their name should only be applied to DBNAMER (not DBNAMER-realm[1-3])
Lets also say all my databases are at build 234 already, so only sql scripts for higher versions (ie 432 and up) should be applied.
I'm currently using the following to get the name of the db to apply the sql to:
Code:
#!/bin/bash
DB_USER="fred"
DB_PASS="wilma"
LOW_BUILD=432
HIGH_BUILD=678
THIS_BUILD=$LOW_BUILD
while [ ${THIS_BUILD} -le ${HIGH_BUILD} ]
do
if [ `expr length "\`find . -name $THIS_BUILD\*\`"` -gt 0 ]
then
for SQL_FILE in `find . -name $THIS_BUILD\*`
do
mysql -u$DB_USER -p$DB_PASS `echo $SQL_FILE | cut -d '_' -f3`$DB_POSTF < $SQL_FILE
done
fi
THIS_BUILD=`expr $THIS_BUILD + 1`
done
Now this only works for DBNAMER files, or if I only had single DBNAMEC and DBNAMEM databases, it would work for them also.
What I'd like to do would be use a ternary operator thusly:
Code:
mysql -u$DB_USER -p$DB_PASS `echo $SQL_FILE | cut -d '_' -f3``printf "%s" (\`echo $SQL_FILE | cut -d '_' -f3\` != "DBNAMER" ? echo -n "-realm1" : echo -n "")` < $SQL_FILE
Which should execute, for example:
Code:
mysql -ufred -pwilma DBNAMEM-realm1 < 432_01_DBNAMEM_tableName.sql
mysql -ufred -pwilma DBNAMEM-realm1 < 456_01_DBNAMEM_tableName.sql
mysql -ufred -pwilma DBNAMEC-realm1 < 543_01_DBNAMEC_tableName.sql
mysql -ufred -pwilma DBNAMEM-realm1 < 543_02_DBNAMEM_tableName.sql
mysql -ufred -pwilma DBNAMER < 543_03_DBNAMER_tableName.sql
mysql -ufred -pwilma DBNAMEM-realm1 < 567_01_DBNAMEM_tableName.sql
mysql -ufred -pwilma DBNAMEC-realm1 < 678_01_DBNAMEC_tableName.sql
This still isn't optimum because I have to hardcode the realm[1-3] bit in, and of course I don't want to run the DBNAMER .sql files more than once, so I'm opened to suggestions on how to do this better.
But my question remains: Is there such a ternary operator?
Thanks,