計算スクリプトのループまで

計算スクリプトのループまで

これは私のコードです。

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ループを正常に作成できますが、最初の2つのif / thesの変数が通過しないことです。どんな提案がありますか?

- - - - - - - - - - - - - - - 完全

さて、目的を達成し、同様の問題を抱えている人を助けるためにここに私がテストした修正されたコードがあります。私はまた、各機能のコードをコメントアウトしましたが、私の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

おすすめ記事