* 业务需求:需要能随机两个单词拼接,并且第二个参数开头要大写,然后再随机一个后缀,生成类似 thisBook_xatk 这样的格式

* 因为需要使用单词库文件,因此默认情况下,使用了 linux 系统的 /usr/share/dict/words 这个单词库

* 创建 random.sh 文件,然后示例代码如下:

#!/bin/sh
#random string
#@param need char length,default 16
randomString()
{
needNum=16
if [ $# -gt 0 ]
then
if [ $1 -gt 0 ]
then
needNum=$1
fi
fi
openssl rand -base64 64 | tr -cd 'a-zA-Z0-9' | sed 's/^[0-9]*//g' | head -c $needNum
}
#count file lines
#@param words file
totalFileLine()
{
if [ $# -lt 0 ]
then
echo "error: totalFileLine need words file param"
exit 1
fi
wc -l $1 | awk '{print $1}'
}
#random one word from file
#@param words file,default /usr/share/dict/words
#@param words total num, default self count
#@param base value,default 37
randomOneWord()
{
wFile=/usr/share/dict/words
ratio=37
if [ $# -gt 0 ]
then
wFile=$1
if [ $# -gt 1 ]
then
if [ $2 -gt 0 ]
then
totalWordsNum=$2
else
totalWordsNum=`totalFileLine $wordsFile`
fi
fi
if [[ $# -gt 2 ]] && [[ $3 -gt 0 ]]
then
ratio=$3
fi
fi
a=$RANDOM
# must declare int
declare -i num
declare -i randNum
num=$(($a*$ratio))
randNum=`expr $num%$totalWordsNum`
#echo $randNum
sed -n "$randNum"p $wFile
}
#random one word from file, make like postnateOvarious_Pe8o(first+Second+_+random)
#@param words file,default /usr/share/dict/words
#@param words total num, default self count
#@param random end num, default 4, if it is 0, there will be no end
#@param base value,default 37
randomOneAssembleWord()
{
wordsFile=/usr/share/dict/words
randomEndNum=4
ratio=37
if [ $# -gt 0 ]
then
wordsFile=$1
if [ $# -gt 1 ]
then
if [ $2 -gt 0 ]
then
totalWordsNum=$2
else
totalWordsNum=`totalFileLine $wordsFile`
fi
fi
if [ $# -gt 2 ]
then
randomEndNum=$3
fi
if [[ $# -gt 3 ]] && [[ $4 -gt 0 ]]
then
ratio=$3
fi
fi
# random first
headWord=`randomOneWord $wordsFile $totalWordsNum $ratio`
#echo "headWord: $headWord"
# random second
secondWord=`randomOneWord $wordsFile $totalWordsNum $ratio`
#echo "secondWord: $secondWord"
# capitalize first letter
secondBigWord=`echo ${secondWord:0:1} | tr '[a-z]' '[A-Z]'`
#echo "secondBigWord: $secondBigWord"
secondWord=$secondBigWord${secondWord:1}
#echo "new secondWord: $secondWord"
# random end
if [ $randomEndNum -gt 0 ]
then
endWord=`randomString $randomEndNum`
endWord="_"$endWord
else
endWord=""
fi
# assemble string
newWord=$headWord${secondWord}${endWord}
echo $newWord
}
#random words from file, then write to result file, make like postnateOvarious_Pe8o(first+Second+_+random)
#@param make num, must > 0
#@param result file, default only echo
#@param words file,default /usr/share/dict/words
#@param base value,default 37
randomWordsToFile()
{
if [ $# -lt 0 ]
then
echo "error: randomWordsToFile need make num param"
exit 1
fi
resultFile=''
wordsFile=/usr/share/dict/words
randomEndNum=4
ratio=37
if [ $# -gt 1 ]
then
resultFile=$2
if [ $# -gt 2 ]
then
wordsFile=$3
fi
if [ $# -gt 3 ]
then
ratio=$4
fi
fi
totalWordsNum=`totalFileLine $wordsFile`
#echo "totalWordsNum: $totalWordsNum"
idx=0
NUM=$1
#echo "randomWordsToFile $NUM $resultFile $wordsFile $ratio"
while (( $idx < $NUM ))
do
# assemble string
newWord=`randomOneAssembleWord $wordsFile $totalWordsNum $randomEndNum $ratio`
#echo "newWord: $newWord"
if [ "$resultFile" = "" ]
then
echo "$newWord"
else
echo "$newWord" >> $resultFile
fi
idx=`expr $idx+1`
done
}

* 使用示例:

#!/bin/sh
wordsFile=/usr/share/dict/words
resultFile=./result.txt
. ./random.sh
#随机3个单词组,并输出到指定文件夹
randomWordsToFile 3 $resultFile $wordsFile
#随机1个单词组,并不需要后缀,如thisBook
randomOneAssembleWord $wordsFile 0 0

点赞(0) 打赏

微信小程序

微信扫一扫体验

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部