====== Colorized Output ======
#!/bin/bash
filename=$1
number_of_words=$2
# Check if a file name and number of words are passed as parameters
if [ $# -lt 2 ]; then
echo "Usage: $0 filename number_of_words [number_of_lines]"
exit 1
fi
# Check if file exists
if [ ! -f $filename ]; then
echo "Error: File $filename does not exist."
exit 1
fi
# Get the total number of words in the file
total_words=$(wc -w $filename | awk '{print $1}')
# Check if the number of words passed as a parameter is not greater than the total number of words in the file
if [ $number_of_words -gt $total_words ]; then
echo "Error: The number of words should be less than or equal to the total number of words in the file ($total_words)."
exit 1
fi
# Define colors array
colors=("\033[31m" "\033[32m" "\033[33m" "\033[34m" "\033[35m" "\033[36m")
# Get the words from the file as an array
all_words=($(cat $filename))
# If the number of lines is not passed as a parameter, output one line
if [ $# -eq 2 ]; then
# Select the required number of random words
for i in $(eval echo {1..$number_of_words}); do
random_index=$((RANDOM % total_words))
selected_word=${all_words[$random_index]}
capitalized_word="$(tr '[:lower:]' '[:upper:]' <<< ${selected_word:0:1})${selected_word:1}"
echo -ne "\033[47m\033[30m\033[1m${colors[$((i - 1)) % ${#colors[@]}]}$capitalized_word \033[0m"
done
echo ""
else
number_of_lines=$3
# Print a line before the output
echo "---"
# Get the required number of random words and combine them into multiple lines, with the number of lines specified
for i in $(eval echo {1..$number_of_lines}); do
# Select the required number of random words
for j in $(eval echo {1..$number_of_words}); do
random_index=$((RANDOM % total_words))
selected_word=${all_words[$random_index]}
capitalized_word="$(tr '[:lower:]' '[:upper:]' <<< ${selected_word:0:1})${selected_word:1}"
echo -ne "\033[47m\033[30m\033[1m${colors[$((j - 1)) % ${#colors[@]}]}$capitalized_word \033[0m"
done
echo ""
done
# Print a line after the output
echo "---"
fi
====== No Color ======
#!/bin/bash
# Check if a file name and number of words are passed as parameters
if [ $# -ne 2 ]; then
echo "Usage: $0 filename number_of_words"
exit 1
fi
filename=$1
number_of_words=$2
# Check if file exists
if [ ! -f $filename ]; then
echo "Error: File $filename does not exist."
exit 1
fi
# Get the total number of words in the file
total_words=$(wc -w $filename | awk '{print $1}')
# Check if the number of words passed as a parameter is not greater than the total number of words in the file
if [ $number_of_words -gt $total_words ]; then
echo "Error: The number of words should be less than or equal to the total number of words in the file ($total_words)."
exit 1
fi
# Get the required number of random words and combine them into a single word, excluding the last underscore
echo $(shuf -n $number_of_words $filename | tr '\n' '_' | sed 's/_$//')
----
Word file:{{ :howtos:wordlist.zip |}}