linux - 类似于linux上的goto

标签 linux bash loops terminal sh

<分区>

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

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

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,我想从输入直接跳到第二部分。

最佳答案

shbash 这样的 Bourne shell 没有 GOTO 语句。这是considered harmful .

相反,使用像 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。

关于linux - 类似于linux上的goto,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19823976/

相关文章:

linux - NFS 缓存清理命令?

linux - 内核不保证编译文件的向后兼容性吗?

linux - 根据条件将 bash 中的 CSV 文件拆分为多个文件

java - 循环验证和输出问题

javascript - 带有上一个、暂停和下一个按钮的 JS 文本循环

c++ - 不匹配的 C++ header 版本

Bash 脚本 - 如果有条件 [ ]

bash - 如何在 Bash 中的 $PATH 中查找文件?

regex - 用于打印模式 1、搜索并打印从模式 2 到模式 3 的所有行以及打印模式 4 的 Bash 脚本

php - 循环中的弹出窗口 - JQuery