setevoy
setevoy

Reputation: 4662

Run sql-command from bash-script?

I created a little script which must change engine for tables:

#!/bin/bash

echo "use test_1;" > query.sql

get_list () {
mysql -u teamcity -ppassword -B -N -e 'show tables like "%"' test_1 | xargs -I '{}'     echo "alter table {} engine=innodb;" >> query.sql
}

get_list ;

mysql -u teamcity -ppassword < query.sql

But - how can I avoid use query.sql file? I make few attempts with "Here document" - but can't solve it...

For example - trying this:

#!/bin/bash

get_list () {
mysql -u teamcity -ppassword -B -N -e 'show tables like "%"' test_1 | xargs -I '{}'     echo "alter table {} engine=innodb;"
}

a="$(get_list)"
b="use test_1;"
c="$b $a"

mysql -u teamcity -ppassword <<< $c

But it is not working...

Upvotes: 0

Views: 3359

Answers (1)

Robin Green
Robin Green

Reputation: 33103

Put everything in a function and pipe that:

#!/bin/bash
get_list () {
  echo "use test_1;"

  mysql -u teamcity -ppassword -B -N -e 'show tables like "%"' test_1 | xargs -I '{}' echo "alter table {} engine=innodb;"
}

get_list | mysql -u teamcity -ppassword 

Upvotes: 3

Related Questions