LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 10-13-2009, 12:16 AM   #1
tifkat
Member
 
Registered: May 2002
Location: Perth, Australia
Distribution: Slack, RH, *BSD, Tru64/DECUnix/OSF1/Ultrix, Solaris/SunOS...
Posts: 36

Rep: Reputation: 16
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,

Last edited by tifkat; 10-13-2009 at 12:18 AM.
 
Old 10-13-2009, 05:24 PM   #2
gzunk
Member
 
Registered: Sep 2006
Posts: 89

Rep: Reputation: 20
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.
 
Old 10-13-2009, 07:28 PM   #3
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
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?

Last edited by ta0kira; 10-13-2009 at 07:33 PM.
 
Old 10-13-2009, 07:40 PM   #4
jlinkels
Senior Member
 
Registered: Oct 2003
Location: Bonaire
Distribution: Debian Lenny/Squeeze/Wheezy/Sid
Posts: 4,053

Rep: Reputation: 484Reputation: 484Reputation: 484Reputation: 484Reputation: 484
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
bash equivalent to C increment operator andrewb758 Linux - General 5 02-21-2009 12:25 PM
if else vs ternary operator hottdogg Programming 2 01-16-2007 11:21 AM
Unary Operator expected. Bash script Blackout_08 Programming 2 06-22-2006 02:21 PM
bash and strings graziano1968 Linux - Software 2 10-01-2004 06:50 AM
bash script and the , operator xviddivxoggmp3 Programming 1 08-14-2004 12:33 AM


All times are GMT -5. The time now is 06:24 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration