이것은 내 코드입니다.
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/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