how to use mysql fields in shell - mysql

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`

Related

BASH MYSQL FETCH FIELDS

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.

Custom #! to run file of SQL commands

I am looking for a way to have a simple way to execute SQL commands. One attempt is to make a custom #! script but I am not sure how to do this either.
If I do something like:
#!/bin/cat -n
select
col1
from
table;
I get output like
1 #!/bin/cat -n
2 select
3 col1
4 from
5 table;
Which makes me think I could be close.
But when I create a script like runsql.sh
#!/bin/bash
cat -n
./some_sql.sh: line 2: syntax error near unexpected token `newline'
./some_sql.sh: line 2: `select'
This is my attempt at being able to execute sql files. Is there someway people are doing this that I am not doing?
Thank you
try it:
mysql -e "YOUR_SQL_COMMAND"
for large command try it (i dont test but need work):
sqlCommand=$(cat <<EOF
This is large
Sql command
This is line three.
EOF
)
mysql < $sqlCommand

Detect and delete line break directly out of mysql query

im trying to detect and delete a line break out of a subject (called m.subject) mail information retrieved via CONCAT out of a mysql database.
That said, the linebreak may or may not occur in the subject and therefore must be detected.
My query looks like this:
mysql --default-character-set=utf8 -h $DB_HOST -D $TARGET -u $DB_USER -p$DB_PW -N -r -e "SELECT CONCAT(m.one,';',m.two,';',m.three,';',m.subject,';',m.four';',m.five,';',(SELECT CONCAT(special_one) FROM special_$SQL_TABLE WHERE msg_id = m.six ORDER BY time DESC LIMIT 1)) FROM mails_$SQL_TABLE m WHERE m.rtime BETWEEN $START AND $END AND m.seven = 1 AND m.eight IN (2);"
I tried to delete it afterwards, but getting in performance trouble due to several while operations on all lines already. Is there an easy way to detect and cut it directly via the CONCAT buildup? It is crucial to retrieve only one line after extraction for me.
Updating/changing the database is not an option for me, as I only want to read the current state.

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