123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- #!/bin/bash
- set -e
- if [ -z $1 ]; then
- echo "usage: addlua.sh path"
- exit -1;
- fi
- IFS=$'\n'
- ROOT=$1
- DIR=`dirname "$0"`
- # 生成比较正经一点的名字, 但需要字典支持
- if [ -f "$DIR/dictionary.txt" ]; then
- WORDs=(`cat "$DIR/dictionary.txt" | cut -d ' ' -f1`)
- fi
- # 生成随机名
- function RANDOM_NAME_FM() {
- if [ -n $WORDs ]; then
- local name=
- while [ ${#name} -lt 16 ]; do
- local index=$(($RANDOM%${#WORDs[@]}))
- name="$name${WORDs[${index}]}"
- done
- # 删除中划线并且将首字母大写
- echo ${name:1}
- else
- # 生成uuid类型的名字
- echo `openssl rand -hex 8`
- fi
- }
- function RANDOM_NAME() {
- if [ -n $WORDs ]; then
- local name=
- while [ ${#name} -lt 16 ]; do
- local index=$(($RANDOM%${#WORDs[@]}))
- name="$name-${WORDs[${index}]}"
- done
- # 删除中划线并且将首字母大写
- echo ${name:1} | awk -F[-] '{for(i=1;i<=NF;i++) printf toupper(substr($i,1,1)) substr($i,2); printf "\n";}'
- else
- # 生成uuid类型的名字
- echo `openssl rand -hex 8`
- fi
- };
- function RANDOM_LUA_CALL() {
- local current=0
- local count=$(($RANDOM%20))
- printf "print('" >> "$1"
- # Generates the file for the specified count.
- while [ $current -lt $total ]; do
- if [ ! $current -eq 0 ]; then
- printf "," >> "$1"
- fi
- printf "%s" "`RANDOM_NAME_FM`" >> "$1"
- let current=current+1
- done
- printf "')\n" >> "$1"
- };
- function RANDOM_LUA_VARIABLE() {
- printf "local `RANDOM_NAME` = '`echo "$RANDOM-$RANDOM-$RANDOM" | base64`'\n" >> "$1"
- };
- function RANDOM_LUA_FUNCATION() {
- local current=0
- local total=$(($RANDOM%20))
- printf "local function `RANDOM_NAME_FM`()\n" >> "$1"
- # Generates the file for the specified count.
- while [ $current -lt $total ]; do
- if [ $(($RANDOM%2)) -eq 0 ]; then
- RANDOM_LUA_VARIABLE $1
- fi
- if [ $(($RANDOM%2)) -eq 0 ]; then
- RANDOM_LUA_CALL $1
- fi
- #if [ $(($RANDOM%50)) -eq 0 ]; then
- # let deep=deep+1
- # RANDOM_LUA_FUNCATION $1
- # let deep=deep-1
- #fi
- let current=current+1
- done
- printf "end\n" >> "$1"
- };
- function RANDOM_LUA_FILE() {
- local name=`RANDOM_NAME`
- while [ -f "$ROOT/$1/${name}.lua" ]; do
- name=`RANDOM_NAME`
- done
- echo "add to $1/${name}.lua"
- local current=0
- local total=$(($RANDOM%20))
-
- # Generates the file for the specified count.
- while [ $current -lt $total ]; do
- if [ $(($RANDOM%5)) -eq 0 ]; then
- RANDOM_LUA_VARIABLE "$ROOT/$1/${name}.lua"
- fi
- if [ $(($RANDOM%5)) -eq 0 ]; then
- RANDOM_LUA_CALL "$ROOT/$1/${name}.lua"
- fi
- if [ $(($RANDOM%5)) -eq 0 ]; then
- RANDOM_LUA_FUNCATION "$ROOT/$1/${name}.lua"
- fi
- let current=current+1
- done
- };
- function RANDOM_LUA_FILES() {
- local files=(`ls "$ROOT/$1"`)
- local current=0
- local total=$((${#files[@]} / 2))
- # Generates the file for the specified count.
- while [ $current -lt $total ]; do
- RANDOM_LUA_FILE "$1"
- let current=current+1
- done
- # Each all dir.
- for file in ${files[@]}; do
- if [ -d "$ROOT/$file" ]; then
- RANDOM_LUA_FILES "$1/$file"
- fi
- done
- };
- RANDOM_LUA_FILES "."
|