今天提供一个可以随机生成手机号的脚本。

#!/bin/bash

function generator(){
	num=$1
	base_str="1234567890"
	for i in `seq 1 8`;do
		pos=$RANDOM%${#base_str}
		char=${base_str:${pos}:1}
		num=$num$char
	done;
	echo $num
}

while getopts "muth" args;
do
	case $args in
		m)
			m_num=""
		    m_s=("134" "135" "136" "137" "138" "139" "147" "150" "151" "152" "157" "158" "159" "165" "172" "178" "182" "183" "184" "187" "188" "198")
			m_pos=$RANDOM%${#m_s[@]}+1
			m_num=${m_s[${m_pos}]}
			generator $m_num
			;;
		u)
			u_num=""
		    u_s=("130" "131" "132" "145" "155" "156" "166" "171" "175" "176" "185" "186")
			u_pos=$RANDOM%${#u_s[@]}+1
			u_num=${u_s[${u_pos}]}
			generator $u_num
			;;
		t)
			t_num=""
		    t_s=("133" "149" "153" "173" "177" "180" "181" "189" "199")
			t_pos=$RANDOM%${#t_s[@]}+1
			t_num=${t_s[${t_pos}]}
			generator $t_num
			;;
		h)
			echo "Usage: generate-mobile [-m|-u|-t]                 Generate a mobile number."
			echo "[-m]: Generate a ChinaMobile mobile number."
			echo "[-u]: Generate a ChinaUnicom mobile number."
			echo "[-t]: Generate a ChinaTelecom mobile number."
			break
			;;
		?)
			echo "illegal option -- $args      you can use genpd -h."
			exit 1
			;;
		esac
done

这个脚本提供了三个参数,-m,-u,-t分别是生成移动号、联通号、电信号。