我遇到的是没有生成彩票。运行脚本后,系统会提示用户输入MaxValue,NumValues(每行数)以及他们喜欢的彩票号码数。这是有效的,直到我添加了检查以确保非数字值,并且NumValues中的数字大于MaxValue导致错误消息。
#!/bin/bash
echo "Please enter a max random number."
read MaxValue
if [[ $MaxValue != +([0-9]) ]]
then
echo "I can't accept any non-numeric characters. Please try again."
exit
fi
echo "Please enter the number of tickets. "
read NumValues
if [ $NumValues -gt $MaxValue ]
then
echo "The number of values can't be greater than the max random number. Please try again."
exit
fi
echo "Please enter how many tickets you'd like. "
read NumSeq
while [ $NumSeq -gt 0 ]
do
touch lotto
x=$NumValues
while [ $x -gt 0 ]
do
num1=$(($RANDOM * MaxValue))
num2=$((num1 / 32767))
if [ `grep -c $num2 lotto` -eq 0 ]
then
echo $num2 >> lotto
x=$((x - 1))
fi
done
NumSeq=$((NumSeq - 1 ))
echo `sort -n lotto`
rm lotto
done