1

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. "
        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"."

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. "
    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

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
    #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
0

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. "
        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 -n "Enter a value for y. "
        read 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)? "
    read yn
 done
| improve this answer | |
  • I used your first suggestion and refined my code. Thanks for your help. – shadowrowan Nov 15 '17 at 3:17
  • I'm glad it worked for you. I wasn't sure if it was clear, so I didn't leave it in. – CoreyJJohnson Nov 15 '17 at 4:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.