Issue with SQL query within a bash script - mysql

I'm having an issue with a query, when I run it from the command line I have no problems, but from a script it expands the script in a strange way.
My query:
QUERY[1]="SELECT users.username AS \"User name\"FROM mytable WHERE creation_date > '${BEGIN}' AND creation_date < '${END}'"
The call from the script:
$(mysql -hmyhost -uuser -pmypass -Dmydb -B -e "${QUERY[${query}]}")
and I get a:
-bash: User: command not found
Any idea whats going wrong?
Thanks

$(mysql -hmyhost -uuser -pmypass -Dmydb -B -e "${QUERY[${query}]}")
I think you don't need to place it under command substitution:
mysql -hmyhost -uuser -pmypass -Dmydb -B -e "${QUERY[${query}]}"
It causes the output of mysql ... to be executed as well thus showing the error User: command not found.

if you just add a line on a script like this:
$(somecommand)
The output of the command 'somecommand' will be used by the shell as a command.
What you probably want to do is:
output=$(somecommand)

Related

Why is this bash variable blank when taking output from mysql?

I am trying to take the output from a MySQL query in bash and use it in a bash variable, but it keeps coming up blank when used in the script, but works perfectly from the terminal. What's wrong here?
I've tried changing the way the statement is written and changing the name of the variable just in case it was somehow reserved. I've also done a significant amount of searching but it turns out if you but 'bash', 'blank', and 'variable' in the search it usually comes up with some version of how to test for blank variables which I already know how to do.
tempo=$(mysql -u "$dbuser" -p"$dbpass" -D "$database" -t -s -r -N -B -e "select user from example where user='$temp' > 0;")
printf "the output should be: $tempo" # This is a test statement
The end result should be that the $tempo variable should either contain a user name from the database or be blank if there isn't one.
I think there is some error with your sql statement at user = '$temp' > 0.
But to get the result from MySql you have to redirect the standard error (stderr) to the standard output (stdout), you should use 2>&1.
Most probably you will run into MySql error but try running this on terminal.
tempo=$((mysql -u "$dbuser" -p"$dbpass" -D "$database" -t -s -r -N -B -e "select user from example where user='$temp' > 0;") 2>&1)
The solution was to echo the result of the sql query like this:
tempo=$(echo $(mysql -u "$dbuser" -p"$dbpass" -D "$database" -s -N -B -e "select user from example where user='$username' > 0;"))
Now I'm left with logic issues but I think I can handle that.

mysql 5.7 -e option get error at line 1: unknown command '\u'

This is the code I wrote on command line:
mysql --user=root --password=root mydb --default-character-set=utf8 -e "set #arg1='[1002,2003,3304]';source run.sql"
However, it throws me this error:
ERROR at line 1: Unknown command '\U'
I have tested if I remove set statement like this and run:
mysql --user=root --password=root mydb --default-character-set=utf8 -e "source run.sql"
The run script file executes.I know I can open the SQL file and change the source code to void this, but this problem is really annoying.
Could anyone have any suggestions to solve this?
Use ; at the end:
... -e "set #arg1='[1002,2003,3304]';source run.sql;"
or change source by \.:
... -e "set #arg1='[1002,2003,3304]';\. run.sql"

Executing mutiple MySQL Queries in bash script

I need to run a monthly bash script via cron that is related to our company's billing system. This is done with two stored procedures. When I run them via the MySQL console and workbench, they work fine.
I've looked at this article and this is basically the way I do it.
I call via cron, a shell script that looks like this:
mysql -h 192.168.1.1 -u<username> -p<password> mydatabase < /path/to/billing_periods.sql
My text file that has the commands in it looks like this:
call sp_start_billing_period();
call sp_bill_clients();
What happens is that the first query runs, but the second one on the second line, doesn't.
I can make a stored procedure that wraps these two - but I just was hoping to learn why this was happening... Perhaps a mistake I made or a limit in the way you do this..
I also considered doing this (two calls to the MySQL shell):
mysql -h 192.168.1.1 -u<username> -p<password> mydatabase -e "call sp_start_billing_period();"
mysql -h 192.168.1.1 -u<username> -p<password> mydatabase -e "call sp_bill_clients();"
You could try separating each statement with a semicolon.
mysql -h 192.168.1.1 -u<username> -p<password> mydatabase -e "call sp_start_billing_period();call sp_bill_clients();"
If you have your statements in a file you can do:
while read LINE; do mysql -u<username> -p<password> mydatabase -e"$LINE";echo "-----------";done < statements.sql
I think you are only allowed to execute a single statement in your input .sql file, see the mysql documentation (manpage) for -e statement.
· --execute=statement, -e statement
Execute the statement and quit. The default output format is like that produced with --batch.
The -e is implicit. At least when I do different mysql queries I put them in their own script like you already suggested.

How does MySQL take variables in bash?

Making a script to print out data from a MySQL db via bash, I met the following problem:
While I try to log in, it uses the password as the database to log in to.
Script is like this:
#!/bin/bash
echo $1
db=$1
pasx=$2
CMD="use $db; select * from job_log;"
mysql -u sqluser -p "${pasx}" -e "$CMD"
If I'm going to run the script with the command
User#server:/path/with/file$ sh sql.sh ok hobo
MySQL returns the following:
User#server:/path/with/file$ sh sql.sh ok hobo
ok
Enter password: ERROR 1049 (42000): Unknown database 'hobo'
I might have fully misunderstood something, but I can't put my finger on what it might be.
You need to remove the space after the -p parameter. See the mysql man page. You also need to specify the database in the command (remove it from the query)
mysql -u sqluser -p$pasx -e "$CMD" $db
Or maybe more clear:
mysql --user=sqluser --password=$pasx --execute="$CMD" $db
Try this:
mysql -u sqluser --password=${pasx} -e "$CMD" $db

Mysql XML table export command line

Maybe I'm not seeing something here but WHY would this command line work perfectly fine and provide a result set when run on a LINUX command line but when executed from a WINDOWS command line it fails dismally and returns nothing?
mysql -hHOSTNAME -uroot -p --xml -e 'SELECT * FROM db.table' > c:\temp\output.xml
What am I missing here?
Windows command line does not recognize ' as a quote character, so your statement parameter becomes SELECT (truncated at the 1st space).
You must use " instead, as Heena Hussain suggested.
Can you please try this...
C:\>mysql -u <userid> -p<password> -e "SHOW VARIABLES LIKE '%version%'" –-xml
and this...
mysql -u db_user -p db_name --xml -e "SELECT * FROM table_name" > table_name.xml