Linux下自动向MySQL中录入数据的脚本

WBOY
풀어 주다: 2016-06-07 17:01:30
원래의
1458명이 탐색했습니다.

#!/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 would 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 would 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"

linux

원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!