Skip to content

Commit

Permalink
Alter techniques for the contributed bug fixes
Browse files Browse the repository at this point in the history
This makes a few changes to the techiques used for the bug fixes
contributed by zqad:

1. Instead of blindly assuming the last element of the arry is null, we
   actually test for that and update the n variable accordingly.

2. The sed command was reverted to the older style, but kept the
   explicit beginning of and end of regex anchors. I was having trouble
   on OS X with the newer-style syntax.

3. The loop to remove the used word from the array was simplified.
  • Loading branch information
elasticdog committed Oct 12, 2014
1 parent b9de92b commit b61494d
Showing 1 changed file with 10 additions and 17 deletions.
27 changes: 10 additions & 17 deletions genhost
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ readonly domain='example.com'

# returns random number from 0 to ($1-1) in global var 'r'.
rand() {
local max=$((32768 / $1 * $1))
local max=$(( 32768 / $1 * $1 ))
while (( (r=$RANDOM) >= max )); do :; done
r=$(( r % $1 ))
}
Expand All @@ -28,11 +28,12 @@ else
fi
fi

# read the word list into memory, removing commented words
# read the word list into memory, removing commented words;
# then remove the trailing empty element, if any:
# http://mywiki.wooledge.org/BashFAQ/005#Handling_newlines_.28or_lack_thereof.29_at_the_end_of_a_file
unset lines n
while IFS= read -r 'lines[n++]'; do :; done < <(sed '/^#/d' "$wordlist")
# since $n gets incremented even by the EOF character, remove 1 from n
n=$(( n - 1 ))
[[ ${lines[n-1]} ]] || unset -v 'lines[--n]'

# output the random hostname(s)
for i in $(seq $1); do
Expand All @@ -43,24 +44,16 @@ for i in $(seq $1); do
# mark the word as used in the word list
case $OSTYPE in
darwin*)
sed -i '' "/^${lines[r]}\$ s/^/#/" "$wordlist"
sed -i '' "s/^${lines[r]}\$/#${lines[r]}/" "$wordlist"
;;
linux*)
sed -i "/^${lines[r]}\$/ s/^/#/" "$wordlist"
sed -i "s/^${lines[r]}\$/#${lines[r]}/" "$wordlist"
;;
esac

# filter the word list to avoid outputting the same hostname twice in one
# session
m=0
unset lines_new
for (( j = 0 ; j < n ; j++)); do
if [ $j -ne $r ]; then
lines_new[$((m++))]=${lines[j]}
fi
done
lines=("${lines_new[@]}")
n=$m
# remove the used word from the array to prevent duplicates
lines=(${lines[@]:0:$r} ${lines[@]:$(( $r + 1 ))})
n=$(( n - 1 ))
done

exit 0

0 comments on commit b61494d

Please sign in to comment.