shell随机生成身份证,姓名,电话,日期,分数,等级和insert语句
#!/bin/bash
#生成随机身份证号,性别,年龄,电话,姓名,日期,分数和对应等级,并生成insert语句
#作者AiYS,2018-02-06,转载请注明http://www.cnblogs.com/AiYS/p/8424334.html
#随机身份证号,性别和年龄
function random_id {
#身份证前6位地区码集合
area_code_collection=($(awk '{print $1}' area_code.txt))
#从地区码集合中随机取一个地区码
area_code=${area_code_collection[$(shuf -i 0-3513 -n 1)]}
#随机生成生日
birthday=$(date +"%Y%m%d" -d "-$(shuf -i 1000-15000 -n 1) days")
#计算当前年份
current_year=$(date +"%Y%m%d" | cut -c 1-4)
#计算生成的身份证号的年份
id_year=$(echo ${birthday} | cut -c 1-4)
#根据当前年份和身份证号年份计算年龄
age=$((${current_year}-${id_year}))
#随机生成身份证15-17的3位顺序码,但是不知范围暂定400
seq=$(shuf -i 100-400 -n 1)
#根据身份证第17位判断性别,奇男偶女
gender_num=$(echo ${seq} | cut -c 3)
[ $((gender_num%2)) -eq 0 ] && gender="F" || gender="M"
#用数组保存身份证号前17位的每一位
for ((i=0,j=1;i<17;i++,j++))
do
array[$i]=$(echo ${area_code}${birthday}${seq} | cut -c $j)
done
sum=0
#用字典保存取模11后余数对映的第18位映射关系
declare -A checkcode_map
checkcode_map=(["0"]="1" ["1"]="0" ["2"]="X" ["3"]="9" ["4"]="8" ["5"]="7" ["6"]="6" ["7"]="5" ["8"]="5" ["9"]="3" ["10"]="2")
#前17位对应的权重值
coefficient=(7 9 10 5 8 4 2 1 6 3 7 9 10 5 8 4 2)
#前17位乘以对应的权重求和
for ((i=0;i<17;i++))
do
sum=$((${sum}+${array[$i]}*${coefficient[$i]}))
done
#和取模11
map=$((sum%11))
#生成第18位校验码
checkcode=${checkcode_map[$map]}
echo -e "${area_code}${birthday}${seq}${checkcode},${gender},${age}" >> id.txt
}
#生成随机电话号码
function random_tel {
pretel=(130 131 132 133 134 135 136 137 138 139 145 147 150 151 152 153 155 156 157 158 159 170 171 173 176 177 178 180 181 182 183 184 185 186 187 188 189)
echo ${pretel[$(shuf -i 0-36 -n 1)]}$(shuf -i 10000000-99999999 -n 1) >> tel.txt
}
#生成随机姓名
function random_name {
fname=(赵 钱 孙 李 周 吴 郑 王 冯 陈 褚 卫 蒋 沈 韩 杨 朱 秦 尤 许 何 吕 施 张 孔 曹 严 华 金 魏 陶 姜 戚 谢 邹 窦 章 云 苏 潘 葛 奚 范 彭 郎 鲁 韦 昌 马 苗 凤 花 方 俞 任 袁 柳 鲍 史 唐 费 廉 岑 薛 雷 贺 倪 汤 滕 殷 罗 毕 郝 安 常 乐 于 傅 齐 康 伍 余 元 顾 孟 平 黄 穆 萧 尹 姚 邵 湛 汪 祁 毛 禹 狄 戴 谈 宋 茅 庞 熊 纪 舒 屈 项 祝 董 梁 杜 阮 蓝 闵 席 季 贾 江 童 颜 郭 梅 盛 林 徐 邱 骆 高 夏 蔡 田 樊 胡 凌 霍 虞 万 柯 管 卢 莫 解 宗 丁 邓 单 洪 包 石 崔 吉 钮 龚 程 嵇 邢 裴 陆 荀 羊 甄 麴 封 芮 羿 储 靳 邴 糜 松 段 伊 刘 景 龙 叶 白 赖 卓 蔺 屠 乔 阳 闻 党 翟 谭 姬 申 郦 牛 扈 燕 温 晏 柴 瞿 阎 习 向 古 廖 寇 聂 晁 曾 司马 上官 欧阳 夏侯 诸葛 东方 赫连 皇甫 尉迟 公羊 澹台 公孙 轩辕 令狐 钟离 宇文 长孙 慕容 司徒 完颜)
lname=($(awk '{print $1}' lname.txt))
echo ${fname[$(shuf -i 0-226 -n 1)]}${lname[$(shuf -i 0-2810 -n 1)]}${lname[$(shuf -i 0-2810 -n 1)]} >> name.txt
}
#生成随机日期和时间
function random_time {
echo "$(date +"%Y-%m-%d" -d "-$(shuf -i 365-10000 -n 1) days"),$(date +"%Y-%m-%d %H:%M:%S" -d "-$(shuf -i 5000-12000 -n 1) days")" >> date.txt
}
#生成随机的分数和对应的等级
function random_score {
integer=$(shuf -i 40-99 -n 1)
#decimal=$(tr -dc 0-9 </dev/urandom | head -c 2)两位随机小数
decimal=$(shuf -i 0-9 -n 1)
score="${integer}.${decimal}"
if [ ${integer} -gt 95 ];then
grade='S'
elif [ ${integer} -gt 85 ];then
grade='A'
elif [ ${integer} -gt 75 ];then
grade='B'
elif [ ${integer} -gt 65 ];then
grade='C'
else
grade='D'
fi
echo "${score},${grade}" >> score.txt
}
#删除之前生成的文件
function delete_file {
[ -f id.txt ] && $(rm id.txt) >/dev/null 2>&1
[ -f name.txt ] && $(rm name.txt) >/dev/null 2>&1
[ -f tel.txt ] && $(rm tel.txt) >/dev/null 2>&1
[ -f date.txt ] && $(rm date.txt) >/dev/null 2>&1
[ -f score.txt ] && $(rm score.txt) >/dev/null 2>&1
}
function generate_records {
read -p "需要生成多少条记录:" count
delete_file
if [ ${count} -lt 0 ];then
echo -e "输入错误!输入的数字小于1\n"
else
for ((m=0;m<${count};m++))
do
random_id
random_name
random_tel
random_time
random_score
#echo "生成第$m条记录"
done
fi
#合并生成的文件用,分隔字段
paste -d "," id.txt name.txt tel.txt date.txt score.txt > records.txt
delete_file
}
function sql {
[ -f student.sql ] && $(rm student.sql) > /dev/null 2>&1
#生成sql插入文件student.sql
outfile='student.sql'
IFS=","
while read id gender age name tel entrance examination score grade
do
cat >> ${outfile} << EOF
INSERT INTO student (id,name,gender,age,tel,entrance,examination,score,grade) VALUES ('${id}','${name}','${gender}',$age,'${tel}',${entrance},${examination},${score},'${grade}');
EOF
done < records.txt
}
generate_records
read -p "是否生成sql文件?y or n:" yn
[ "$yn" = "y" -o "$yn" = "Y" ] && sql || echo "Byebye"
shell随机生成身份证,姓名,电话,日期,分数,等级和insert语句的更多相关文章
- Java 随机生成中文姓名,手机号,邮编,住址
package lovo; import java.util.HashMap; import java.util.Map; /** * 随机生成中文姓名,性别,Email,手机号,住址 * @auth ...
- PHP随机生成中国人姓名的类
PHP随机生成类 <?php /*rndChinaName.class.php*/ Class rndChinaName { private $arrXing,$numbXing; privat ...
- Shell随机生成字符串
随机生成18位的字符串,数字 大小写字符 斜线 password=`openssl rand -base64 |-`
- SQLServer 随机生成指定范围的日期
一个分页的问题,DTCms3.0中,分页是根据时间分页的,如果当添加时间(add_time)都是同一个数值时,不管点击第几页,显示的数据都是同一个的内容,于是就有了需要把同一个时间改指定随机日期的功能 ...
- c#随机生成中文姓名
为什么要自己写这个生成器呢?大家应该都有过为测试数据发愁的时候,我就是出于这样的原因. 尽管本次代码很少,但是还会有后续的生成器分享出来. 我代码底子还不是很好,希望各位同道能够发表意见,同是也欢迎大 ...
- SqlServer 随机生成中文姓名(转)
,) )) -- 姓氏 ,) )) -- 名字 INSERT @fName VALUES ('赵'),('钱'),('孙'),('李'),('周'),('吴'),('郑'),('王'),('冯'),( ...
- Golang 随机生成中国人姓名
package main import ( "fmt" "math/rand" "time" ) var lastName = []stri ...
- java 随机生成身份证代码
import java.util.Calendar; import java.util.Collection; import java.util.HashMap; import java.util.I ...
- Python 随机生成有效手机号码及身份证
中国那么大,人那么多,几乎人手一部手机.手机号码已经作为各大互联网站的注册账户.同样,身份证更是如此.以下是生成有效手机号码和身份证号. 身份证需要下载districtcode.txt这个文件:htt ...
随机推荐
- Error: expected expression, got '}'
1.错误描述 Error: expected expression, got '}' .globalEval/<@http://localhost:8080/Sys/resource/globa ...
- Reactor-反应器模式
Reactor模式:反应器模式,是高性能网络服务器中最为常用的一种模式,libevent,muduo,libuv等网络库都是以 Reactor模式构建.Reactor模式由同步事件多路分解器和具体事件 ...
- Apache Hive 基本理论与安装指南
一.Hive的基本理论 Hive是在HDFS之上的架构,Hive中含有其自身的组件,解释器.编译器.执行器.优化器.解释器用于对脚本进行解释,编译器是对高级语言代码进行编译,执行器是对java代码的执 ...
- PortableApps使用入门
PortableApps使用入门 Software 介绍 添加软件 绿软下载站推荐 介绍 官网:http://portableapps.com/ PortableApps作为一款卓越的绿软管理软件,它 ...
- mybatis的Mapper文件配置
一.resultMap resultMap 元素是 MyBatis 中最重要最强大的元素. 该配置节点下如下子节点配置 id – 一个 ID 结果;标记结果作为 ID 可以帮助提高整体效能 const ...
- redux (一)
redux 是一个状态管理的库. redux认为页面所有的变化,都是基于状态的改变触发的,所以我们维护一个应用的时候,都是在维护这些状态.而 redux 就是为了维护状态而生的. API create ...
- C#程序入门学习
前言: C# (C sharp) 是微软对这一问题的解决方案.C#是一种最新的.面向对象的编程语言.它使得程序员可以快速地编写各种基于Microsoft .NET平台的应用程序,Microsoft . ...
- npm打包前端项目太慢问题分析以及暂时解决方案
npm build 打包前端项目实际上是执行 node build/build.js,但是随着项目的依赖包越来越多,项目打包时间不断延长,为了改善这个问题,需要从node入手 暂时解决方案:扩大nod ...
- [UWP]理解ControlTemplate中的VisualTransition
1. 前言 VisualTransition是控件模板中的重要组成部分,无论是自定义控件或者修改控件样式都会接触到VisualTransition.明明这么重要,博客园上好像都没多少关于VisualT ...
- Cannot resolve taglib with uri http://java.sun.com/jsp/jstl/core
问题 <Spring 实战>第5章,在 IDEA 中 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" pre ...