unknown option '--print-defaults' from bash script [duplicate] - mysql

This question already has answers here:
Reading quoted/escaped arguments correctly from a string
(4 answers)
Closed 3 years ago.
I'm trying to run a query from a bash script:
#!/bin/bash
query="\"show databases\""
command="mysql --defaults-file=/user/.my.cnf -e "
outputfile=" > query_result.txt"
command=$command$query$outputfile
$($command)
the result is this:
# ./query_test
mysql: unknown option '--print-defaults'
what I'm doing wrong?
The command:
mysql --defaults-file=/user/.my.cnf -e "show databases"
works without any issue from the shell

thanks to the comment of #benjamin-w I solved with this:
#!/bin/bash
args=(--defaults-file=/users/.my.cnf)
args+=(-e "show databases")
outputfile="query_result.txt"
mysql "${args[#]}" > "$outputfile"
other examples in this link:
https://mywiki.wooledge.org/BashFAQ/050

Related

Bash mysql not being executed [duplicate]

This question already has answers here:
Why does shell ignore quoting characters in arguments passed to it through variables? [duplicate]
(3 answers)
Closed 1 year ago.
I'm constructing a Bash script to execute a MySQL query. The Bash scrip is very simple, but the query is not being executed correctly. MySQL responds like a do a mysql usage (help of commands). What am I doing wrong?
The bash file is:
COMANDO='mysql -h 148.72.64.68 -p******** -u root db_vias_ue -e "select count(*) from clientes"'
$COMANDO
Bash quoting rules are seriously weird. In what you wrote, the expression within double quotes actually gets separated into multiple arguments.
Try this:
COMANDO='mysql -h 148.72.64.68 -p******** -u root db_vias_ue -e "select count(*) from clientes"'
bash -c "$COMANDO"

Issues with mysql command in bash script [duplicate]

This question already has answers here:
Expansion of variables inside single quotes in a command in Bash
(8 answers)
Closed 3 years ago.
I'm really struggling with a bash command, trying to launch a mysql query :
output=$(mysql --user=root --password="$password" -s --execute 'grant all privileges on zabbix.* to zabbix#localhost identified by "$zabbix_pwd";')
The statement above does not work, because $zabbix_pwd is not take as a variable, as it should...
Any idea on how to rewrite this statement would be very helpful.
Cheers,
Raj.
Your escaping is wrong. Please try this :
output=$(mysql --user=root --password="$password" -s --execute "grant all privileges on zabbix.* to zabbix#localhost identified by \"$zabbix_pwd\";")
Hope it helps.

Parse the output of a mysql command into a variable in a bash script [duplicate]

This question already has answers here:
Problem with return variable in bash
(3 answers)
Closed 8 years ago.
I'm trying to use a bash script to sanitize a database and I need to use the largest ID Number from the users table so I have this line in my script
MAXID=$(mysql -u root -proot elis27 -e "select max(idnumber) from mdl_user;")
echo $MAXID
And the output of that line in my script is
max(idnumber) 3
How can I parse the output of the mysql command so that MAXID is just 3?
Use the --skip-column-names (or -N for short) option to omit column name headings in the output:
MAXID=$(mysql -u root -proot -N elis27 -e "select max(idnumber) from mdl_user;")
I'll let you put the awk statement in maxid declaration, here is simple logic to get 3 -
a="max(idnumber) 3"
b=`echo $a | awk '{print $2}'`;echo $b

BASH script not reading input mysql password [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to execute Mysql command from a shell script?
I'm trying to execute a .sql file using a bash script. But I am having a problem connecting to MySQL. Here's what I have so far:
#! /bin/sh
PWD="thepassword"
mysql -p -u theuser < Randomsqlfile.sql
echo $PWD
When the password is prompted, nothing fills out.
Make this:
mysql -utheuser -pthepassword <Randomsqlfile.sql

How do you get table-formatted output from MySQL in non-interactive mode? [duplicate]

This question already has answers here:
MYSQL differs in Output from script
(2 answers)
Closed 3 years ago.
I like the table output that the mysql client program produces in interactive mode, but if I try to run a sql script like this:
mysql -uroot mydb < myscript.sql
I only get tab-separated output.
mysql -uroot mydb -e 'select * from mytable'
does produce the output in the desired table format, however.
How can I get the first command to produce table-formatted output? I don't want HTML output, but the terminal character output with aligned columns and headers.
Add the -t option to mysql (table).
mysql -t -uroot mydb < myscript.sql
mysql -t -uroot mydb -e 'select * from mytable'
Use \P less -S option before running the query
mysql> \P less -S
PAGER set to 'less -S'