#!/bin/bash
set -x
count=0
number=0
loops=0
average=0
read -p " Please enter a number between 1 and 100? " number
while [ $count -lt $number ]
done
average=`expr $score / $number`
echo $average
创建一个 shell 程序,要求用户输入 1 到 100 之间的数字,并在输入标记值 -1 后退出循环。程序必须保存所有循环迭代的计数、总计数以及程序结束后的平均次数。
我猜我必须使用一个if
声明,但我不太知道该怎么做。
答案1
可能有更有效的方法可以做到这一点,但以下脚本应该可以完成这项工作:
#!/bin/bash
loops=0
sum=0
# Loop forever
while true; do
read -p "Please enter a number between 1 and 100: " input
# Check for the exit condition.
if [[ $input -eq -1 ]]; then
break
fi
# Use a regex to check that input is a one-digit, two-digit or three-digit number and that the input is in [1,100] .
if [[ $input =~ ^[0-9]{1,3}$ && $input -gt 0 && $input -le 100 ]]; then
# Use arithmetic expansion to increment loop counter and add the new input to sum.
((loops++))
((sum += input))
else
echo "Invalid input. Skipping..."
fi
done
# For the edge case where there is no valid input.
if [[ $loops -eq 0 ]]; then
echo "Average undefined - no valid input."
exit
fi
average=$((sum/loops))
echo "Average: $average"
示例运行:
Please enter a number between 1 and 100: 2
Please enter a number between 1 and 100: 3
Please enter a number between 1 and 100: 4
Please enter a number between 1 and 100: 5
Please enter a number between 1 and 100: 6
Please enter a number between 1 and 100: -1
Average: 4
答案2
我没有正确理解你的要求。尽管如此,我还是尽力做到最好。如果您需要其他内容,请在下面发表评论,以便我可以编辑此答案。
这是外壳脚本:
$ cat sentinel_2.sh
score=0
loops=0
average=0
while :
do
read -p "Please enter a number between 1 and 100 : " number
if ! [ "$number" -eq "$number" ] 2> /dev/null
then
echo "Not integer"
elif [ $number -eq -1 ]
then
echo "You entered -1, program exits"
break
elif [ $number -gt 100 ] || [ $number -lt 1 ]
then
echo "Out of range"
else
loops=`expr $loops + 1`
score=`expr $score + $number`
echo 'iteration= '$loops' and score = '${score}
fi
done
echo 'Final score = ' $score
echo 'Total iterations= ' $loops
if [ $score -eq 0 ] || [ $loops -eq 0 ]
then
echo "score and iteration are needed to calculate average"
else
echo 'Average = Final Score / Total iterations'
average=$(($score / $loops))
echo 'Average is = '${average}
fi
示例输出:
$ sh sentinel_2.sh
Please enter a number between 1 and 100 : 3
iteration= 1 and score = 3
Please enter a number between 1 and 100 : 5
iteration= 2 and score = 8
Please enter a number between 1 and 100 : 2
iteration= 3 and score = 10
Please enter a number between 1 and 100 : abcd
Not integer
Please enter a number between 1 and 100 : 102
Out of range
Please enter a number between 1 and 100 : 4
iteration= 4 and score = 14
Please enter a number between 1 and 100 : -1
You entered -1, program exits
Final score = 14
Total iterations= 4
Average = Final Score / Total iterations
Average is = 3
具有负面情况的示例输出:
$ sh sentinel_2.sh
Please enter a number between 1 and 100 :
Not integer
Please enter a number between 1 and 100 :
Not integer
Please enter a number between 1 and 100 :
Not integer
Please enter a number between 1 and 100 : abc
Not integer
Please enter a number between 1 and 100 : 100
iteration= 1 and score = 100
Please enter a number between 1 and 100 : 102
Out of range
Please enter a number between 1 and 100 : -1
You entered -1, program exits
Final score = 100
Total iterations= 1
Average = Final Score / Total iterations
Average is = 100
$ sh sentinel_2.sh
Please enter a number between 1 and 100 : 0
Out of range
Please enter a number between 1 and 100 : 102
Out of range
Please enter a number between 1 and 100 : abcd
Not integer
Please enter a number between 1 and 100 : -1
You entered -1, program exits
Final score = 0
Total iterations= 0
score and iteration are needed to calculate average
答案3
#!/bin/bash
declare -i num total avg cnt ;
while read -p "Enter a number: " num ; do
[[ "$num" == "" ]] || [[ $num -eq 0 ]] || [[ $num -gt 100 ]] || [[ $num -lt -1 ]] && echo "ERROR: Out of range" && continue;
[ $num -eq -1 ] && break ;
((cnt++));
((total+=$num));
avg=$(($total/$cnt));
done ;
echo "Count: $cnt Total: $total Avg: $avg"
答案4
而不是使用if
- 语句:
#!/bin/bash
while IFS= read -r -p 'Enter number 1-100: '
do
case $REPLY in
-1)
break
;;
[1-9]|[1-9][0-9]|100)
sum=$(( sum + REPLY ))
nums=$(( nums + 1 ))
;;
*)
printf '%s is not a valid input\n' "$REPLY" >&2
esac
done
printf '\n'
printf 'Sum of %d numbers is %d\n' "$nums" "$sum"
printf 'Avg of %d numbers is %d\n' "$nums" "$(( sum/nums ))"
这使用模式[1-9]|[1-9][0-9]|100
来匹配有效输入(数字 1-9、10-99 和 100)。它还允许用户通过按 退出循环Ctrl+D,这通常用于表示输入结束。