shell 脚本定时创建月份表

时间:2024-04-09 09:06:50

#!/bin/sh
user='root'
pass='root'
name='vfc_sport'

# 数据表名定义
timestamp=`date -d "next month" +%Y%m`
tablename='vf_sport_'$timestamp

# SQL语句
mysql -uroot -proot <<EOF
USE $name

CREATE TABLE IF NOT EXISTS $tablename (
id int(11) NOT NULL,
user_id int(11) NOT NULL,
bracelet_id int(11) NOT NULL,
mode tinyint(1) NOT NULL COMMENT '0为白天,1为晚上',
step int(11) NOT NULL COMMENT '步行数量',
time varchar(100) NOT NULL COMMENT '时间,单位为秒',
calorie int(11) NOT NULL COMMENT '卡路里',
distance int(11) NOT NULL COMMENT '距离,单位米',
create_date date NOT NULL,
create_time timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
is_sync int(11) NOT NULL COMMENT '0为没同步,1为同步了'
) ENGINE=MyISAM AUTO_INCREMENT=8 DEFAULT CHARSET=utf8 COMMENT='运动数据表';

EOF
exit;

注:

1.shell 脚本,=号前后都不能留空格

2.变量声明不是带$,在使用的时候,需要带$

3.不能带`,倒引号