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 Code:
DBNAMEC-realm1 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 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 Code:
mysql -ufred -pwilma DBNAMEM-realm1 < 432_01_DBNAMEM_tableName.sql But my question remains: Is there such a ternary operator? Thanks, |
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.
|
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? |
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. |