LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Ternary operator for strings in BASH (https://www.linuxquestions.org/questions/programming-9/ternary-operator-for-strings-in-bash-761525/)

tifkat 10-13-2009 12:16 AM

Ternary operator for strings in BASH
 
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,

gzunk 10-13-2009 05:24 PM

Sorry I can't be more helpful... But have you considered using Python or Perl instead of Bash? The languages are much richer and would likely solve your problem more elegantly.

ta0kira 10-13-2009 07:28 PM

I usually solve this sort of thing with a multi-stage loop/grep operation where I break the data down into various line-based files and recombine them as many times as are necessary to get the data organized how I need it. In most cases that's a solution far easier to deal with than the headache of trying to avoid temporary files. Additionally, it allows you to make corrections without redoing everything, and to branch out for side tasks.
Kevin Barry

PS Doing some things without intermediate files, especially with a lot of data, can come with a cost of a magnitude greater processing time. This, of course, really only applies if your ternary operator isn't available in a shell-scriptable language. Maybe csh?

jlinkels 10-13-2009 07:40 PM

I would not try to do everything with a single statement. First I would use AWK to split the file name in version number, realm number database name and table name. Once you have those variables, apply some simple tests when to use the realm suffix and when not.

You might have to use AWK multiple times to get all fields into BASH variables. That takes a bit more time but speed at this part of the script is irrelevant.

I can't give you an example, I am typing this on a handheld and that is hard. If you don't know AWK, look in the manuak, this function is really in chapter 1 and easy to do. Do set the FS to '_' though.

jlinkels


All times are GMT -5. The time now is 11:10 PM.