Prevent string splitting.

This commit is contained in:
Nicolás A. Ortega Froysa 2024-12-31 17:45:52 +01:00
parent ff0632fc0f
commit d49aabc499

39
d6.sh
View File

@ -44,7 +44,7 @@ function print_help() {
} }
function test_case() { function test_case() {
if [ $1 != "lower" ] && [ $1 != "upper" ] && [ $1 != "capital" ] if [ "$1" != "lower" ] && [ "$1" != "upper" ] && [ "$1" != "capital" ]
then then
return 1 return 1
else else
@ -62,52 +62,59 @@ do
case $arg in case $arg in
v) v)
echo "d6 v$VERSION" echo "d6 v$VERSION"
exit 0;; exit 0
;;
h) h)
print_help print_help
exit 0;; exit 0
;;
n) n)
NUM_WORDS=$OPTARG;; NUM_WORDS=$OPTARG
;;
f) f)
WORDLIST="$OPTARG";; WORDLIST="$OPTARG"
;;
d) d)
DELIMITER="$OPTARG";; DELIMITER="$OPTARG"
;;
c) c)
if test_case $OPTARG if test_case "$OPTARG"
then then
CASE="$OPTARG" CASE="$OPTARG"
else else
echo "'$OPTARG' is an invalid case. Use 'capital', 'lower', or 'upper'." echo "'$OPTARG' is an invalid case. Use 'capital', 'lower', or 'upper'."
exit 1 exit 1
fi;; fi
;;
?) ?)
print_usage print_usage
exit 2 exit 2
;;
esac esac
done done
WORDS="" WORDS=""
for i in $(seq 1 $NUM_WORDS) for i in $(seq 1 "$NUM_WORDS")
do do
ROLLS="" ROLLS=""
for j in {1..5} for j in {1..5}
do do
ROLLS="$((1 + $RANDOM % 6))$ROLLS" ROLLS="$((1 + RANDOM % 6))$ROLLS"
done done
if [ $CASE == "upper" ] if [ "$CASE" == "upper" ]
then then
WORD="$(awk "/$ROLLS/{ print toupper(\$2) }" $WORDLIST)" WORD="$(awk "/$ROLLS/{ print toupper(\$2) }" "$WORDLIST")"
elif [ $CASE == "lower" ] elif [ "$CASE" == "lower" ]
then then
WORD="$(awk "/$ROLLS/{ print tolower(\$2) }" $WORDLIST)" WORD="$(awk "/$ROLLS/{ print tolower(\$2) }" "$WORDLIST")"
else else
WORD="$(awk "/$ROLLS/{ print \$2 }" $WORDLIST)" WORD="$(awk "/$ROLLS/{ print \$2 }" "$WORDLIST")"
WORD="${WORD^}" WORD="${WORD^}"
fi fi
if [ $i -eq $NUM_WORDS ] if [ "$i" -eq "$NUM_WORDS" ]
then then
WORDS="${WORDS}${WORD}" WORDS="${WORDS}${WORD}"
else else