# Until Loop in a Calculation Script

So here's my code:

``````if test \$# -eq 2
then
x=\$1
y=\$2

echo "You entered "\$x" for x and "\$y" for y"
else
if test \$# -eq 1
then
x=\$1
echo -n "Enter a value for y. "
echo
echo "You entered "\$x" for x."
echo "You entered "\$y" for y."
else
echo -n "Enter a value for x. "
echo "You entered "\$x"."
echo
echo -n "Enter a value for y. "
echo "You entered "\$y"."
fi
fi
echo
((a=y+x))
echo
((b=y-x))
echo "Your first and second number subtracted from each other equals "\$b"."
echo
((c=y*x))
echo "Your first and second number multiplied together equals "\$c"."
echo
((d=y/x))
echo "Your first and second number divided from each other equals "\$d"."
echo
((e=y%x))
echo "Your first and second number have a remainder from division that equals "\$e"."
``````

So what I'm trying to do is loop a request for input after the if/then/else statements run and then have the variables pass through the calculations before looping back to the input request again.

So something like:

``````until [[ \$yn == "n" ]]
echo -n "Enter value for x. "
echo -n "Enter value for y. "
#The calculation steps in the code above.
echo -n "Do you want to crunch some more numbers (y/n)? "
done
``````

My problem is I can get the else to loop fine by having the while loop after it and including the calculation functions, but the variables from the first two if/then's aren't passing through. Any suggestions?

---------------------------Done

Okay, so just for completions sake and to help if anyone has a similar problem, here is my revised code that I've tested that works. I've also commented in the code for each function, my Linux teacher likes that, so if anyone notices something incorrect please make mention of it.

``````#Keep looping the script until the user enters 'n'
yn="not n"
until [[ \$yn == "n" ]]
do
#Check if two inputs are supplied after the command
if test \$# -ge 2
then
#Assign the two variables
x=\$1
y=\$2
#Echo back what was entered for x and y
echo "You entered "\$x" for x and "\$y" for y."
echo
#If two inputs weren't provided, check if one was
elif test \$# -eq 1
#Assign the first argument to x
then x=\$1
echo -n "Enter a value for y. "
echo
#Echo back what was entered for x and y
echo "You entered "\$x" for x."
echo "You entered "\$y" for y."
echo
#If neither one or two inputs were provided, ask the user for both
else
echo -n "Enter a value for x. "
#Echo back what was entered for x
echo "You entered "\$x"."
echo
echo -n "Enter a value for y. "
#Echo back what was entered for y
echo "You entered "\$y"."
echo
fi
((a=y+x))
echo
#Subtraction
((b=y-x))
echo "Your first and second number subtracted from each other equals "\$b"."
echo
#Multiplication
((c=y*x))
echo "Your first and second number multiplied together equals "\$c"."
echo
#Division
((d=y/x))
echo "Your first and second number divided from each other equals "\$d"."
echo
#Division giving a remainder
((e=y%x))
echo "Your first and second number have a remainder from division that equals "\$e"."
echo
echo
#Set the number of arguments counted to zero to skip the first two variable checks on looping
while [ \$# -gt 0 ]
do
shift
done
#Ask if the user wants to calculate another set of variables and loop back to asking for both inputs unless the user inputs 'n'
echo -n "Do you want to crunch some more numbers (y/n)? "
done
``````

You could put the whole thing in your until loop. Remove arguments at the end.

`````` #!/usr/bin/sh

yn="not n"
until [[ \$yn == "n" ]]
do

#Your x/y definition script from above
if test \$# -ge 2
then
x=\$1
y=\$2
echo "You entered "\$x" for x and "\$y" for y"
elif test \$# -eq 1
then x=\$1
echo -n "Enter a value for y. "
echo
echo "You entered "\$x" for x."
echo "You entered "\$y" for y."
else
echo -n "Enter a value for x. "
echo "You entered "\$x"."
echo -n "Enter a value for y. "
fi

#The calculation steps in the code above.

# remove arguments before potentially repeating
while [ \$# -gt 0 ]
do
shift
done

echo -n "Do you want to crunch some more numbers (y/n)? "