1

所以我正在linux终端上编写一个程序,我的程序有两个部分。第一部分是除法,第二部分是计算一些数字的 MOD。退出第一部分的方法是将 999 放入任何一个要除的输入中。

我的问题是即使用户输入 999 作为第一个输入,用户也必须输入第二个数字。我想知道这些是否类似于在 Windows 中可以执行 goto :someOtherLocation 在 linux 中的东西。这是代码:

echo "Enter the number to divide (dividend) (enter 999 to quit):"
read numberOne
[IF NUMBERONE = 999, JUMP TO SECONDPART]
echo "Enter the number to divide (divisor) (enter 999 to quit):"
read numberTwo

while [ "$numberOne" -ne '999' ] && [ "$numberTwo" -ne '999' ]
do
    while [ "$numberTwo" -eq 0 ]
    do
    echo "You cannot divide by 0, please enter another number:"
    read numberTwo
    done

RESULT=$(echo "$numberOne/$numberTwo" | bc -l)
echo $numberOne / $numberTwo = $RESULT
echo $numberOne / $numberTwo = $RESULT >> results.txt
echo "Enter the number to divide (dividend) (enter 999 to quit):"
read numberOne
echo "Enter the number to divide (divisor) (enter 999 to quit):"
read numberTwo
done

SECONDPART

counter=1
totalCount=0
temporal=0

while [ "$counter" -lt '101' ]
do
temporal=$( expr $counter % 5)
echo $counter MOD 5 = $temporal
echo $counter MOD 5 = $temporal >> results.txt
totalCount=$(echo "$totalCount+$temporal" | bc -l)
counter=$(echo "$counter+1" | bc -l)
done

average=$(echo "$totalCount/100" | bc -l)
echo The average of all the MODs is $average >> results.txt

如上所示,如果输入是 999,我想直接从输入跳到第二部分。

4

2 回答 2

3

Bourne shell 喜欢sh并且bash没有 GOTO 语句。它被认为是有害的。

相反,使用结构化流控制,如 if 语句:

echo "Enter the number to divide (dividend) (enter 999 to quit):"
read numberOne
if [ "$numberOne" -ne 999 ]
then
  echo "Enter the number to divide (divisor) (enter 999 to quit):"
  read numberTwo

  while [ "$numberOne" -ne '999' ] && [ "$numberTwo" -ne '999' ]
  do
      while [ "$numberTwo" -eq 0 ]
      do
      echo "You cannot divide by 0, please enter another number:"
      read numberTwo
      done

      RESULT=$(echo "$numberOne/$numberTwo" | bc -l)
      echo $numberOne / $numberTwo = $RESULT
      echo $numberOne / $numberTwo = $RESULT >> results.txt
      echo "Enter the number to divide (dividend) (enter 999 to quit):"
      read numberOne
      if [ "$numberOne" -ne 999 ]
      then
          echo "Enter the number to divide (divisor) (enter 999 to quit):"
          read numberTwo
      fi
  done
fi

counter=1
totalCount=0
temporal=0

while [ "$counter" -lt '101' ]
do
    temporal=$( expr $counter % 5)
    echo $counter MOD 5 = $temporal
    echo $counter MOD 5 = $temporal >> results.txt
    totalCount=$(echo "$totalCount+$temporal" | bc -l)
    counter=$(echo "$counter+1" | bc -l)
done

average=$(echo "$totalCount/100" | bc -l)
echo The average of all the MODs is $average >> results.txt

它可能看起来比一个简单的 goto 更尴尬,但是当您利用更结构化的控制流时,代码变得更易于阅读和遵循:

readNumber() {
  local number
  read -p "$1" number
  [ "$number" -ne 999 ] && echo "$number"
}

while one=$(readNumber "Enter dividend or 999 to quit: ") && \
      two=$(readNumber "Enter divisor or 999 to quit: ")
do
    echo "$one / $two = $(echo "$one / $two" | bc -l)" | tee results.txt
done

这会一直要求除数,直到用户为其中任何一个输入 999。

于 2013-11-06T22:24:53.307 回答
2

您也可以执行以下操作(bash 示例):

#!/bin/bash

secondpart() {
    echo "Hello"
}

read x
if [ $x -eq 999 ]
then
    secondpart
else 
    echo "Bye"
fi

现在,当你运行代码时,如果你的输入是999程序将返回Hello,否则返回Bye

于 2013-11-06T22:34:20.137 回答