addlua.sh 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. #!/bin/bash
  2. set -e
  3. if [ -z $1 ]; then
  4. echo "usage: addlua.sh path"
  5. exit -1;
  6. fi
  7. IFS=$'\n'
  8. ROOT=$1
  9. DIR=`dirname "$0"`
  10. # 生成比较正经一点的名字, 但需要字典支持
  11. if [ -f "$DIR/dictionary.txt" ]; then
  12. WORDs=(`cat "$DIR/dictionary.txt" | cut -d ' ' -f1`)
  13. fi
  14. # 生成随机名
  15. function RANDOM_NAME_FM() {
  16. if [ -n $WORDs ]; then
  17. local name=
  18. while [ ${#name} -lt 16 ]; do
  19. local index=$(($RANDOM%${#WORDs[@]}))
  20. name="$name${WORDs[${index}]}"
  21. done
  22. # 删除中划线并且将首字母大写
  23. echo ${name:1}
  24. else
  25. # 生成uuid类型的名字
  26. echo `openssl rand -hex 8`
  27. fi
  28. }
  29. function RANDOM_NAME() {
  30. if [ -n $WORDs ]; then
  31. local name=
  32. while [ ${#name} -lt 16 ]; do
  33. local index=$(($RANDOM%${#WORDs[@]}))
  34. name="$name-${WORDs[${index}]}"
  35. done
  36. # 删除中划线并且将首字母大写
  37. echo ${name:1} | awk -F[-] '{for(i=1;i<=NF;i++) printf toupper(substr($i,1,1)) substr($i,2); printf "\n";}'
  38. else
  39. # 生成uuid类型的名字
  40. echo `openssl rand -hex 8`
  41. fi
  42. };
  43. function RANDOM_LUA_CALL() {
  44. local current=0
  45. local count=$(($RANDOM%20))
  46. printf "print('" >> "$1"
  47. # Generates the file for the specified count.
  48. while [ $current -lt $total ]; do
  49. if [ ! $current -eq 0 ]; then
  50. printf "," >> "$1"
  51. fi
  52. printf "%s" "`RANDOM_NAME_FM`" >> "$1"
  53. let current=current+1
  54. done
  55. printf "')\n" >> "$1"
  56. };
  57. function RANDOM_LUA_VARIABLE() {
  58. printf "local `RANDOM_NAME` = '`echo "$RANDOM-$RANDOM-$RANDOM" | base64`'\n" >> "$1"
  59. };
  60. function RANDOM_LUA_FUNCATION() {
  61. local current=0
  62. local total=$(($RANDOM%20))
  63. printf "local function `RANDOM_NAME_FM`()\n" >> "$1"
  64. # Generates the file for the specified count.
  65. while [ $current -lt $total ]; do
  66. if [ $(($RANDOM%2)) -eq 0 ]; then
  67. RANDOM_LUA_VARIABLE $1
  68. fi
  69. if [ $(($RANDOM%2)) -eq 0 ]; then
  70. RANDOM_LUA_CALL $1
  71. fi
  72. #if [ $(($RANDOM%50)) -eq 0 ]; then
  73. # let deep=deep+1
  74. # RANDOM_LUA_FUNCATION $1
  75. # let deep=deep-1
  76. #fi
  77. let current=current+1
  78. done
  79. printf "end\n" >> "$1"
  80. };
  81. function RANDOM_LUA_FILE() {
  82. local name=`RANDOM_NAME`
  83. while [ -f "$ROOT/$1/${name}.lua" ]; do
  84. name=`RANDOM_NAME`
  85. done
  86. echo "add to $1/${name}.lua"
  87. local current=0
  88. local total=$(($RANDOM%20))
  89. # Generates the file for the specified count.
  90. while [ $current -lt $total ]; do
  91. if [ $(($RANDOM%5)) -eq 0 ]; then
  92. RANDOM_LUA_VARIABLE "$ROOT/$1/${name}.lua"
  93. fi
  94. if [ $(($RANDOM%5)) -eq 0 ]; then
  95. RANDOM_LUA_CALL "$ROOT/$1/${name}.lua"
  96. fi
  97. if [ $(($RANDOM%5)) -eq 0 ]; then
  98. RANDOM_LUA_FUNCATION "$ROOT/$1/${name}.lua"
  99. fi
  100. let current=current+1
  101. done
  102. };
  103. function RANDOM_LUA_FILES() {
  104. local files=(`ls "$ROOT/$1"`)
  105. local current=0
  106. local total=$((${#files[@]} / 2))
  107. # Generates the file for the specified count.
  108. while [ $current -lt $total ]; do
  109. RANDOM_LUA_FILE "$1"
  110. let current=current+1
  111. done
  112. # Each all dir.
  113. for file in ${files[@]}; do
  114. if [ -d "$ROOT/$file" ]; then
  115. RANDOM_LUA_FILES "$1/$file"
  116. fi
  117. done
  118. };
  119. RANDOM_LUA_FILES "."