【问题标题】:Something similar to goto on linux [duplicate]类似于 linux 上的 goto [重复]
【发布时间】:2013-11-18 09:24:45
【问题描述】:

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

【问题讨论】:

  • 正常的if 语句有什么问题?
  • “Linux”和“Linux 终端”不是编程语言。我想你的意思是“Bash”。
  • @IgnacioVazquez-Abrams ,没什么,我想做一个 If,但我问如果它是一个 '999' 的情况下在 if 里面放什么,所以它跳到第二部分代码。
  • 你没有。只有当它不是时,你才让它运行第一部分。
  • @IgnacioVazquez-Abrams 哦,所以if 999 ( do the first part), else jump to second?听起来可以解决问题

标签: linux bash loops terminal sh


【解决方案1】:

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。

【讨论】:

  • 谢谢,两种解决方案都可以正常工作。我也更喜欢第一个,因为它在编码时更流畅,即使它不是我最初要求的。
【解决方案2】:

您也可以这样做(bash 示例):

#!/bin/bash

secondpart() {
    echo "Hello"
}

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

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

【讨论】:

  • 谢谢,这正是我所要求的,但另一个答复有更好的解决方案,
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-09-25
  • 2013-09-22
  • 1970-01-01
  • 2015-04-09
  • 1970-01-01
  • 2018-04-04
  • 1970-01-01
相关资源
最近更新 更多