shell脚本同时删除多张表

来源:互联网 发布:2016淘宝童装店铺排名 编辑:程序博客网 时间:2024/06/08 12:12
#同时删除表名类似的多张表
#参数1是数据库名 参数2传表名


#!/bin/sh
if [ $# -ne 2 ];then
echo "input argument dbname universaltablename"
exit 1;
fi
mysqlConnect='mysql'

droptable=`$mysqlConnect -e "SELECT CONCAT( 'DROP TABLE ', GROUP_CONCAT(table_name) , ';' ) 
AS statement FROM information_schema.tables WHERE table_schema = '$1' AND table_name LIKE '$2%';" | grep -v statement`         
#注意使用~键中的那个点,将删除表的命令复制给droptable_sql变量

echo $droptable;    #打印要删除所有表
$mysqlConnect -e"use $1;${droptable}"   #执行删除命令