Então aqui está meu código:
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. "
read y
echo
echo "You entered "$x" for x."
echo "You entered "$y" for y."
else
echo -n "Enter a value for x. "
read x
echo "You entered "$x"."
echo
echo -n "Enter a value for y. "
read y
echo "You entered "$y"."
fi
fi
echo
((a=y+x))
echo "Your first and second number added together equals "$a"."
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"."
Então, o que eu estou tentando fazer é repetir uma solicitação de entrada depois que as instruções if / then / else forem executadas e, em seguida, as variáveis passarem pelos cálculos antes de retornarem ao pedido de entrada novamente.
Então, algo como:
until [[ $yn == "n" ]]
echo -n "Enter value for x. "
read x
echo -n "Enter value for y. "
read y
#The calculation steps in the code above.
echo -n "Do you want to crunch some more numbers (y/n)? "
read yn
done
O meu problema é que consigo fazer com que a outra pessoa faça um loop bem, tendo o loop while após ele e incluindo as funções de cálculo, mas as variáveis dos dois primeiros se / então não estão passando. Alguma sugestão?
--------------------------- Concluído
Ok, apenas por causa das conclusões e para ajudar se alguém tiver um problema semelhante, aqui está o meu código revisado que testei e que funciona. Eu também comentei no código para cada função, meu professor de Linux gosta disso, então, se alguém perceber algo incorreto, por favor, mencione isso.
#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
#Ask for y
echo -n "Enter a value for y. "
read 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
#Ask for x
echo -n "Enter a value for x. "
read x
#Echo back what was entered for x
echo "You entered "$x"."
echo
#Ask for y
echo -n "Enter a value for y. "
read y
#Echo back what was entered for y
echo "You entered "$y"."
echo
fi
#Addition
((a=y+x))
echo "Your first and second number added together equals "$a"."
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)? "
read yn
done