何千もの単語のリストから 4 つの単語のランダムなパスフレーズを生成する単純な bash スクリプトを作成しました。個人的に使用するのが本当に安全で効率的かどうかはわかりませんが、改善について考えている場合はお知らせください。しかし、それは重要なポイントではありません。それをチェックしてください - >
したがって、ラップトップで実行すると、入力と出力は次のようになります。
time sh genpass
astrology cringe tingling massager
real 0m0.319s
user 0m0.267s
sys 0m0.077s
二度目:
$ time sh genpass
prankish askew siren fritter
real 0m0.318s
user 0m0.266s
sys 0m0.077s
時々かなり面白いことがあります。
とにかく、これはスクリプトです:
# EDITABLES ###########################################
target="/path/to/my/wordList.txt"
# END EDITABLES #######################################
getWordList() {
case $1 in
"verb") mawk '/ing$|ed$|en$/ {print $2}' $target ;;
"adjective") mawk '/y$|ish$/ {print $2}' $target ;;
"noun") mawk '!/ing$|ed$|en$|y$|ish$/ {print $2}' $target ;;
*) printf "%s" "'${1}' is an invalid argument." && echo && exit 1
esac
}
pickRandomLineNumber() {
# Get the list in an array
declare -a list_a=("${!1}")
# How many items in the list
local length="${#list_a[@]}"
# Generate a random number between 1 and the number of items in the list
local number=$RANDOM
let "number %= $length"
# Print the word at random line
printf "%s\n" ${list_a[@]} | mawk -v line=$number 'NR==line {print}'
}
read -ra verbList <<< $( getWordList verb )
verb=$(pickRandomLineNumber verbList[@])
read -ra adjectiveList <<< $( getWordList adjective )
adjective=$(pickRandomLineNumber adjectiveList[@])
read -ra nounList <<< $( getWordList noun )
noun1=$(pickRandomLineNumber nounList[@])
noun2=$(pickRandomLineNumber nounList[@])
printf "%s %s %s %s\n" "${adjective}" "${noun1}" "${verb}" "${noun2}"
単語の種類ごとに配列を作成する必要がある場所がわかりますか? 3タイプ3配列。そのコードを関数で取得することを考えたので、その関数を 4 回 (4 つの単語ごとに 1 つずつ) 異なる引数で呼び出す必要があります。私はそれがもっと速いだろうと本当に思っていました。
コードの変更は次のとおりです。
# EDITABLES ###########################################
target="/path/to/my/wordList.txt"
# END EDITABLES #######################################
getWordList() {
case $1 in
"verb") mawk '/ing$|ed$|en$/ {print $2}' $target ;;
"adjective") mawk '/y$|ish$/ {print $2}' $target ;;
"noun") mawk '!/ing$|ed$|en$|y$|ish$/ {print $2}' $target ;;
*) printf "%s" "'${1}' is an invalid argument." && echo && exit 1
esac
}
pickRandomLineNumber() {
# Get the list in an array
declare -a list_a=("${!1}")
# How many items in the list
local length="${#list_a[@]}"
# Generate a random number between 1 and the number of items in the list
local number=$RANDOM
let "number %= $length"
# Print the word at random line
printf "%s\n" ${list_a[@]} | mawk -v line=$number 'NR==line {print}'
}
#### CHANGE ####
getWord() {
read -ra list <<< $( getWordList $1)
local word=$(pickRandomLineNumber list[@])
printf "%s" "${word}"
}
printf "%s %s %s %s\n" $(getWord adjective) $(getWord noun) $(getWord verb) $(getWord noun)
ここに入力/出力があります:
$ time sh genpass
overstay clench napping palace
real 0m0.403s
user 0m0.304s
sys 0m0.090s
そしてまた:
$ time sh genpass
gainfully cameo extended nutshell
real 0m0.369s
user 0m0.304s
sys 0m0.090s
タイミングの違いはそれほど大きな問題ではありませんが、全体的には間違いなく速くなると思いました.
では、2 番目のスクリプトが最初のスクリプトよりも遅い理由がわかりますか?