1

So I am writing a program on linux terminal and my program has two parts. The first part is division, the second part is calculating the MOD for some numbers. The way to quit the first part is to put 999 in either of the inputs to divide.

My problem is that the user must put a second number even if the user input 999 as a first input. I was wondering if these is a thing like in windows where you can do goto :someOtherLocation in linux. This is the code:

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

So as seen above, I would like to jump from the input straight to the secondpart, if the input is 999.

that other guy
  • 116,971
  • 11
  • 170
  • 194
Jason Marks
  • 247
  • 1
  • 4
  • 11

2 Answers2

3

Bourne shells like sh and bash don't have GOTO statements. It's considered harmful.

Instead, use structured flow control like if statements:

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

It may seem more awkward than one simple goto, but as you take advantage of more structured control flow, the code gets easier to read and follow:

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

This keeps asking for numbers to divide, until the user enters 999 for either of them.

that other guy
  • 116,971
  • 11
  • 170
  • 194
  • Thanks, both solutions work fine. I like the first one better as well because it flows better with the coding, even if it not what I was initially asking for. – Jason Marks Nov 06 '13 at 22:52
2

You can also do something like this (bash example):

#!/bin/bash

secondpart() {
    echo "Hello"
}

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

Now, when you run the code, if your input is 999 the program will return Hello, otherwise the return will be Bye.

Coneone
  • 292
  • 1
  • 8