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"
Related
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
This question already has answers here:
How do I set a variable to the output of a command in Bash?
(15 answers)
Closed 6 years ago.
Lets say I have a query first that inserts certain values in a table and the next query is to display the maximum value of one of the columns and then store that in a variable. I then need to display that variable such that it shows the max value.
For example:
sudo mysql -u$mysql_user -p$mysql_pwd -h $mysql_host --database $db_name -e "INSERT INTO service_status_batch VALUES ();"
batch_id= sudo mysql -u$mysql_user -p$mysql_pwd -h $mysql_host --database $db_name -e "SELECT MAX(id) as maxid FROM service_status_batch;"
echo "Value of the id is:" $batch_id
This echo command should then show the value of the variable. However it ends up showing me the value of the select query in the form of a table and not the value of the variable.
Is there a particular way to assign the query value to a variable in shell script?
I have attached the select query value that it shows.
Use -s and -N options with mysql command like this.
sudo mysql -u$mysql_user -p$mysql_pwd -h $mysql_host --database $db_name -e "INSERT INTO service_status_batch VALUES ();"
batch_id=`sudo mysql -u$mysql_user -p$mysql_pwd -h $mysql_host --database $db_name -s -N -e "SELECT MAX(id) as maxid FROM service_status_batch;"`
echo "Value of the id is:" $batch_id
Refer the details for -s and -N :
--silent, -s
Silent mode. Produce less output. This option can be given multiple
times to produce less and less output.
This option results in nontabular output format and escaping of
special characters. Escaping may be disabled by using raw mode; see
the description for the --raw option.
--skip-column-names, -N
Do not write column names in results.
EDIT3: Bad explanation - I was trying to show how to get the value considering it could be used as necessary:
sudo echo $(echo "SELECT MAX(id) as maxid FROM service_status_batch" | mysql dbnamehere -uUser -pPassword)
EDIT1: variable version obviously
EDIT2: corrected variable assignment by using shellcheck.net as suggested. thanks.
EDIT3: one last edit to add sudo right before mysql command as it won't work without it for users other than root.
batch_id=$(echo "SELECT MAX(id) as maxid FROM service_status_batch" | sudo mysql dbnamehere -uUser -pPassword)
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
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
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'