计算脚本中的 Until 循环

计算脚本中的 Until 循环

这是我的代码:

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

所以我想做的是在 if/then/else 语句运行后循环输入请求,然后让变量通过计算,然后再次循环回输入请求。

所以像这样:

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

我的问题是我可以通过在 else 后面加上 while 循环并包含计算函数来使 else 正常循环,但是前两个 if/then 中的变量没有通过。有什么建议么?

- - - - - - - - - - - - - -完毕

好的,为了完成目的并帮助任何人遇到类似的问题,这里是我已经测试过的修改后的代码。我还在每个函数的代码中进行了注释,我的 Linux 老师喜欢这样,所以如果有人发现不正确的地方请指出。

#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

答案1

你可以把整个事情放在你的until循环中。删除最后的参数。

 #!/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

相关内容