Passing variables to SQL script in bash -



Passing variables to SQL script in bash -

i writing script in bash uses sql file run queries setup database , table structure.

i need pass user inputted info script, have nail brick wall. line using run sql script:

mysql --user="root" --password="$mysql_password" --execute "set @domain=${domain}" < mailserver-databases.sql

an illustration of 1 of commands in 'mailserver-database.sql' file needs variable passed is:

insert `servermail`.`virtual_domains`(`id` ,`name`)values('1', '@domain');

where '@domain' user inputted value - 'domain' set next command:

echo -n 'please come in domain wish utilize > ' read domain

when come in 'test.com' domain, comes next error:

error 1109 (42s02) @ line 1: unknown table 'test' in field list

so looks variable beingness passed script, i'm assuming there syntax error somewhere in script

here sql file output:

drop database if exists servermail; create database servermail; grant select on servermail.* 'usermail'@'127.0.0.1' identified 'mailpassword'; flush privileges; utilize servermail; create table `virtual_domains` (`id` int not null auto_increment,`name` varchar(50) not null,primary key (`id`)) engine=innodb default charset=utf8; create table `virtual_users` (`id` int not null auto_increment,`domain_id` int not null,`password` varchar(106) not null,`email` varchar(120) not null,primary key (`id`),unique key `email` (`email`),foreign key (domain_id) references virtual_domains(id) on delete cascade) engine=innodb default charset=utf8; create table `virtual_aliases` (`id` int not null auto_increment,`domain_id` int not null,`source` varchar(100) not null,`destination` varchar(100) not null,primary key (`id`),foreign key (domain_id) references virtual_domains(id) on delete cascade) engine=innodb default charset=utf8; insert `servermail`.`virtual_domains`(`id` ,`name`)values('1', '@domain');

i don't think in case execute commands -e|--execute executes commands sent standard input.

you can couple them this:

echo -n 'please come in domain wish utilize > ' read domain { echo "set @domain='${domain}';"; cat mailserver-databases.sql; } | mysql --user="root" --password="$mysql_password"

also if want utilize variable's value not wrap variable in quotes:

insert `servermail`.`virtual_domains`(`id` ,`name`) values ('1', @domain);

bash

Comments

Popular posts from this blog

assembly - What is the addressing mode for ld, add, and rjmp instructions? -

vowpalwabbit - Interpreting Vowpal Wabbit results: Why are some lines appended by "h"? -

Is there a way to convert an HTML page styled with Bootstrap CSS into email-compatible html? -