简体   繁体   中英

Loop through result set of mysql in Bash - routines

I want to make specific .sql file for each of my routines.

#!/bin/bash

 routine_names=$(mysql mydb  --execute="SELECT 
    *
FROM
    information_schema.routines
WHERE
    routine_type = 'PROCEDURE' OR routine_type ='FUNCTION'
        AND routine_schema = 'mydb';"|cut -f1)

for routine in "$routine_names"
do
    if [ -e "${routine}.sql" ]
     then
    echo "ok"
    else
     content_procedure=$(mysql mydb--execute="SHOW CREATE PROCEDURE $routine;")
     echo "$content_procedure" >> masoud.txt
   fi   
done 

my routine_names variable is a list of my procedures. like this:

SP_ONE
SP_TWO
SP_THREE

I want to loop of these result. but I think the result is not an array. because routine variable has all content.

wrap your mysql mydb ... command with ()

 routine_names=($(mysql mydb  --execute="SELECT 
    *
FROM
    information_schema.routines
WHERE
    routine_type = 'PROCEDURE' OR routine_type ='FUNCTION'
        AND routine_schema = 'mydb';"|cut -f1))

I was curious and got the idea from here .

Just another case how I fix my problem

#!/bin/bash
mysql -u USER -p -h localhost -D database1 -e "SELECT ID FROM prodTable WHERE display=1 AND new=1 AND exDate<DATE_SUB(CURDATE(),INTERVAL 2 YEAR)" | while read ID;
do 
      echo "The following product has been moved: $ID"
done

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM