Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The idea is that the number is chosen by the user and the user chooses to either

ID: 3695401 • Letter: T

Question

The idea is that the number is chosen by the user and the user chooses to either have counted from zero to the number or backwards from the number to zero, here is what I got so far

echo -n "Enter a number: "
read NUM
echo "Enter U to count up to $NUM and D to count down to 0: "
read choice

if [ $choice == "U" ]
then
counter=0
until [ $counter -eq $NUM ]
do
counter=`expr $counter + 1`
echo $counter


elif [ $choice == "D" ]
then
counter=$NUM
while [ $counter != 0 ]
do
counter=`expr $counter - 1`
echo $counter
fi
done

Explanation / Answer

The idea is that the number is chosen by the user and the user chooses to either have counted from zero to the number or backwards from the number to zero

Solution :

echo -n "Enter a number: "
read NUM
echo "Enter U to count up to $NUM and D to count down to 0: "
read choice
if [ $choice == "U" ]
then
counter=0
until [ $counter -eq $NUM ]
do
counter=`expr $counter + 1`
echo $counter
done
elif [ $choice == "D" ]
then
counter=$NUM
while [ $counter != 0 ]
do
counter=`expr $counter - 1`
echo $counter
done
fi

Result :

sh-4.3$ bash -f main.sh    Enter a number: 5 Enter U to count up to 5 and D to count down to 0: U 1 2 3 4 5

Mistake in your code : Did not complete the until do statement and while do statments with Done. I have added done after while do and until do statements.

Syntax :