Script for automatically entering data into MySQL in Linux

Source: Internet
Author: User
Tags recode
#! Binsh # definethestrctureoftablequot; (usernamechar (50) notnull, passwordchar (20) notnull, uidchar (10) no

#! /Bin/sh # define the strcture of tableTABLE = quot; (username char (50) not null, password char (20) not null, uid char (10) no

#! /Bin/sh
# Define the strcture of table
TABLE = "(username char (50) not null, password char (20) not null, uid char (10) not null, gid char (10) not null, maildir char (100) not null, domain char (20) not null, active char (1) default '1 ');"
# Create database and table
# ++ ++
For type in database table; do
Printf "Please input the $ type name you wowould like to create: [postfix]"
While read data; do
If [! -Z $ {data}]; then
If ["$ type" = "database"]; then
Database = $ data
Elif ["$ type" = "table"]; then
Table = $ data
Fi
Break
Fi
Printf "Please input the database name you wowould like to create: [postfix]"
Done
Done

# ++ ++
# Input the data per user
# ++ ++
For (I = 1; I ++); do
Printf "Do you want to input the data per user: [yes, no]"
While read yes_no; do
If ["$ yes_no "! = "Yes"]; then break 2; fi
Echo "Please input the data of NO. $ I user :"
For col in username password uid gid maildir domain active; do
Printf "$ col of NO. $ I:" & read $ {col} [$ {I}]
Done
Break
Done
Done
# Output the data you input
# ++ ++
Echo ""
Echo "++ ++ ++"
Echo "Please make sure the data you input :"
Echo "Database: $ database"
Echo "Table: $ table"
Echo ""
Echo "Show the recodes you input :"
Echo "username password uid gid maildir domain active"
For (n = 1; n Echo "$ {username [$ n] }$ {password [$ n] }$ {uid [$ n] }$ {gid [$ n] }$ {maildir [$ n] }$ {domain [$ n] }$ {active [$ n]}"
Done
# ++ ++
# Execute the SQL to input data to mysql
# ++ ++
Echo ""
Echo "Executing the SQL to input data to mysql ......"
Printf "Creating database $ database ......"&&\
Mysql-u root-h -- password = RedHat-e "create database if not exists $ {database }"&&\
Echo-e "\ 033 [32mOK \ 033 [m"
Printf "Creating table $ table ......"&&\
Mysql-u root-h -- password = redhat $ database-e "create table if not exists $ {table} $ TABLE "&&\
Echo-e "\ 033 [32mOK \ 033 [m"
# Insert recode to mysql
For (n = 1; n Printf "Insert NO. $ n recode ......"&&\
Mysql-u root-h -- password = redhat $ database-e "insert into $ table values ('$ {username [$ n]}', encrypt ('$ {password [$ n]}'), '$ {uid [$ n]}', '$ {gid [$ n]}', '$ {maildir [$ n]}', '$ {domain [$ n]}', '$ {active [$ n]}') "& \
Echo-e "\ 033 [32mOK \ 033 [m"
Done
# Show all the recode
Echo ""
Echo "Show all the recods in the mysql :"
Mysql-u root-h -- password = redhat $ database-e "select * from $ table"

,

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.