#!/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
ユーザーに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通常、入力終了信号に使用されるを押してループを終了できます。