BASH MYSQL FETCH FIELDS - mysql

i have this code:
for file in $(ls -I *.bad1 -I *.bad2 $1); do
query="select file_name,dest_path,new_file_name from FILES where
file_name='"${file%%\_*}"'"
while read -a row
do
name="${row[0]}"
dest="${row[1]}"
new_name="${row[2]}"
echo $name
echo $dest
echo $new_name
done < <(echo $query | mysql -N -u root -pcorollario86 -D test)
done
It work but the select statement i need is:
select max(file_name),max(dest_path),max(new_file_name) from FILES where
file_name='"${file%%\_*}
because i have to compare the return value of each field from the statement (>0 or =0).
The problem is that when i use this second statement BASH give me an error
regard the use of aggregation function.
I DON'T NEED TO PRINT EACH FIELDS. I NEED TO FETCH EACH FIELD INTO A VARIABLE.
Exists another way to fetch every single field from select statement into single variable?
Any suggestion please?
Thanks in advance.

Related

Mysql query to update multiple rows using a input file from linux

I'm trying to update multiple rows in a DB using a small script.
I need to update the rows based on some specific user_ids which I have in a list on Linux machine.
#! /bin/bash
mysql -u user-ppassword db -e "update device set in_use=0 where user_id in ()";
As you see above, the user_ids are in a file, let's say /opt/test/user_ids_txt.
How can I import them into this command?
This really depends on the format of user_ids_txt. If we assume it just happens to be in the correct syntax for your SQL in statement, the following will work:
#! /bin/bash
mysql -u user-ppassword db -e "update device set in_use=0 where user_id in ($(< /opt/test/user_ids_txt))";
The bash interpreter will substitute in the contents of the file. This can be dangerous for SQL queries, so I would echo out the command on the terminal to make sure it is correct before implementing it. You should be able to preview your SQL query by simply running the following on the command line:
echo "update device set in_use=0 where user_id in ($(< /opt/test/user_ids_txt))"
If your file is not in the SQL in syntax you will need to edit it (or a copy of it) before running your query. I would recommend something like sed for this.
Example
Let's say your file /opt/test/user_ids_txt is just a list of user_ids in the format:
aaa
bbb
ccc
You can use sed to edit this into the correct SQL syntax:
sed 's/^/\'/g; s/$/\'/g; 2,$s/^/,/g' /opt/test/user_ids_txt
The output of this command will be:
'aaa'
,'bbb'
,'ccc'
If you look at this sed command, you will see 3 separate commands separated by semicolons. The individual commands translate to:
1: Add ' to the beginning of every line
2: Add ' to the end of every line
3: Add , to the beginning of every line but the first
Note: If your ID's are strictly numeric, you only need the third command.
This would make your SQL query translate to:
update device set in_use=0 where user_id in ('aaa'
,'bbb'
,'ccc')
Rather than make a temporary file to store this, I would use a bash variable, and simply plug that into the query like this:
#! /bin/bash
in_statement="$(sed 's/^/\'/g; s/$/\'/g; 2,$s/^/,/g' /opt/test/user_ids_txt)"
mysql -u user-ppassword db -e "update device set in_use=0 where user_id in (${in_statement})";

only a string is returned in a mysql query in a bash script

There's plenty of similar questions out there that seem to answer this, but it's not working for me.
Here's mysql query in a bash script, the resultant row comes back as a string, not as an array.
Am I missing something?
while read -a row;
do
echo "${row[0]}";
done < <(mysql -u $dbuser -p$dbpass -N --database=$dbname --host=$dbhost --batch -se "SELECT id, CONCAT(id, '_', filename) from photos" );
}
This echoes 200 200_filename.jpg.
I would think it is supposed to echo 200.
Echoing ${row[1]} displays a blank line.
And the magic spell is the IFS. Adding that in with the tab fixes the array issue
while IFS=$'\t' read -a row;
do
echo "${row[0]}";
done

How to convert MySQL query output in array in shell scripting?

I am storing output of MySQL query in a varible using shell scripting. The output of SQL query is in multiple rows. When I checked the count of the variable (which I think is an array), it is giving 1. My code snippet is as follows:
sessionLogin=`mysql -ugtsdbadmin -pgtsdbadmin -h$MYSQL_HOST -P$MYSQLPORT CMDB -e " select distinct SessionID div 100000 as 'MemberID' from SessionLogin where ClientIPAddr like '10.104%' and LoginTimestamp > 1426291200000000000 order by 1;"`
echo "${#sessionLogin[#]}"
How can I store the MySQL query output in an array in shell scripting?
You can loop over the output from mysql and append to an existing array. For example, in Bash 3.1+, a while loop with process substitution is one way to do it (please replace the mysql parameters with your actual command)
output=()
while read -r output_line; do
output+=("$output_line")
done < <(mysql -u user -ppass -hhost DB -e "query")
echo "There are ${#output[#]} lines returned"
Also take a look at the always excellent BashFaq

Bash Script Evaluate String Problems

I'm trying to create a script to automatically delete all of the tables from a database using shell.
The commented out variable $drop works fine, however when I try to substitute in the table
for table in $tables
do
command="'drop table ${table}'"
# drop=$(${login} -e 'drop table test') -- this works fine
drop=$(${login} -e $command)
echo $drop
# echo -e "Removed table ${table}"
done
(major edit)
The issue is with your use of quotes. In your code, since you do not quote $command it is subject to word splitting by the shell. The $login command receives these arguments: "-e", "'drop", "table", "table_name'" -- note the stray single quotes in the second and last elements.
Do this:
command="drop table $table"
drop=$($login -e "$command")

how to use mysql fields in shell

I have table ,and i need access them from shell script and do the following.
> USERNAME="usr" PASSWORD="pwd"
> DBNAME="mydb"
>
> mysql -u$USERNAME -p$PASSWORD $DBNAME<<EOF
> selectfield1,field2,field3,field4 from table;
> EOF
gives me the records.But i need to process each fields with normal linux commands-
for example;
> mysql -u$USERNAME -p$PASSWORD $DBNAME<<EOF
> select field1,field2,field3,field4 from table;
> EOF
> scp field1#field2: field3#field4:/tmp && rm -rf field2
something like that - basic stuff is to use the database records for invoking linux commands on local machine.
One approach would be to use this construct (try it out, the syntax is a bit weird):
cat << EOF | while read a b c ; do echo "a:$a b:$b c:$c" ; done
one two three
EOF
[Edit: obviously replace cat and one two three with your actual sql stuff]
You will need to make sure that mysql does not print anything other than the result (no logo/copyright notice/version info header etc...) - I believe there are command line switches for that.
You'll also run into problems if there are spaces in the data returned, or if it's separated by something else than just whitespace. For those, you can usually get away with setting $IFS to something that never happens in your columns (maybe |), and making your query output that character between two fields.
Thanks for the answers and suggestions - But I found following method much more easier.
Is it safe to use this method?
variable=`mysql -u$USERNAME -p$PASSWORD $DBNAME <<EOF
select name,pass,email,flag from UserRemap where flag="Y" ;
EOF`
echo $variable
name=`echo $variable | cut -d' ' -f5`
pass=`echo $variable | cut -d' ' -f6`
mail=`echo $variable | cut -d' ' -f7`