批量建库脚本

今天被shell脚本的单双引号搞晕了,一般来说单双引号是没有问题的,但如果引号里面套引号就要引起注意了,今天被这个坑的好惨,记录下:

#!/bin/bash


USERNAME="root"
PASSWORD="X5online@docker"
echo "开始建立class表"
mysql -u$USERNAME -p$PASSWORD -e "
use school_info;
DROP TABLE IF EXISTS class;
CREATE TABLE class (
cid int NOT NULL AUTO_INCREMENT,
caption varchar(100) NOT NULL,
PRIMARY KEY (cid)
) ENGINE=Innodb DEFAULT CHARSET=utf8mb4;

INSERT INTO class (caption) VALUES('三年二班');
INSERT INTO class (caption) VALUES('一年二班');
INSERT INTO class (caption) VALUES('三年一班');

"
if [ $? -eq 0 ];then
    echo "创建class表完成,并添加完数据"
fi

echo "开始建立student表"
mysql -u$USERNAME -p$PASSWORD -e "
use school_info;
DROP TABLE IF EXISTS student;
create table student (
sid int NOT NULL AUTO_INCREMENT,
sname varchar(20) NOT NULL,
gender varchar(10) NOT NULL,
class_id int NOT NULL,
PRIMARY KEY (sid)
) ENGINE=Innodb DEFAULT CHARSET=utf8mb4;
INSERT INTO student (sname,gender,class_id) VALUES('范冰冰','女','1');
INSERT INTO student (sname,gender,class_id) VALUES('佟丽娅','女','1');
INSERT INTO student (sname,gender,class_id) VALUES('刘辉煌','男','2');

"
if [ $? -eq 0 ];then
    echo "创建student表完成,并添加完数据"
fi

echo "开始建立teacher表"
mysql -u$USERNAME -p$PASSWORD -e "
use school_info;
DROP TABLE IF EXISTS teacher;
create table teacher (
tid int NOT NULL AUTO_INCREMENT,
tname varchar(20) NOT NULL,
PRIMARY KEY (tid) 
) ENGINE=Innodb DEFAULT CHARSET=utf8mb4;

INSERT INTO teacher (tname) VALUES('alex');
INSERT INTO teacher (tname) VALUES('林海峰');
INSERT INTO teacher (tname) VALUES('武沛齐');
"

if [ $? -eq 0 ];then
    echo "创建teacher表完成,并添加完数据"
fi


echo "开始建立course表"
mysql -u$USERNAME -p$PASSWORD -e "
use school_info;
DROP TABLE IF EXISTS course;
create table course (
cid int NOT NULL AUTO_INCREMENT,
cname varchar(20) NOT NULL,
teacher_id int NOT NULL,
PRIMARY KEY (cid)
) ENGINE=Innodb DEFAULT CHARSET=utf8mb4;

INSERT INTO course (cname,teacher_id) VALUES('生物','1');
INSERT INTO course (cname,teacher_id) VALUES('体育','1');
INSERT INTO course (cname,teacher_id) VALUES('物理','2');

"
if [ $? -eq 0 ];then
    echo "创建course表完成,并添加完数据"
fi




echo "开始建立score表"
mysql -u$USERNAME -p$PASSWORD -e "
use school_info;
DROP TABLE IF EXISTS score;
create table score (
sid int NOT NULL AUTO_INCREMENT,
student_id int NOT NULL,
course_id int NOT NULL,
number int,
PRIMARY KEY (sid)
) ENGINE=Innodb DEFAULT CHARSET=utf8mb4;


INSERT INTO score (student_id,course_id,number) VALUES('1','1','60');
INSERT INTO score (student_id,course_id,number) VALUES('1','2','59');
INSERT INTO score (student_id,course_id,number) VALUES('2','2','100');
"
if [ $? -eq 0 ];then
    echo "创建score表完成,并添加完数据"
fi

  

posted @ 2016-12-27 13:56  梦轻尘  阅读(283)  评论(0编辑  收藏  举报