DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Execute List Of Sql Files

10.22.2011
| 2676 views |
  • submit to reddit
        // script to generate the correct commands to execute the sql statements from a text file. The text file script_list.txt should start with schema_name followed by a space and then script_file_path.

#!/bin/sh
# script to generate the correct commands to execute the sql statements from a text file
# the text file script_list.txt should start with schema_name followed by a space and then script_file_path
# disable echo command and the double quotes to execute the statements

myhost='10.10.10.420'
myuser='root'
mypass='root@123'
myport='3306'
mylog='/home/company/logs'
mkdir -p $mylog

cat /home/company/script_list.txt | while read -r myschema filename
do
if [ -s $filename ];then
mybase=`basename $filename | awk -F"." '{print $1}'`
echo "time mysql -h$myhost -u$myuser -p$mypass $myschema -vvv -f --show-warnings < $filename > "$mylog"/"$myschema"_"$mybase"_success.txt 2> "$mylog"/"$myschema"_"$mybase"_err.txt"
#echo "$myschema $filename"

else
echo " FILE $filename DOES NOT EXIST"
fi

done

exit

$ cat script_list.txt
incometax /home/company/account/change_scripts_o2_1.sql
incometax /home/company/account/change_scripts_o2_11_06_2011.sql
incometax /home/company/account/change_scripts_o2_21_06_2011.sql
incometax /home/company/account/change_scripts_o2_27_06_2011.sql


# The commands to analyse logs

ls -lhS *_err.txt | more
ls -lhS *_err.txt | wc -l
ls -lhS *_success.txt | wc -l
grep -v "Warnings: 0" * |  grep -B9 -i warning | more