0

I'm wrote a simple script that converts the users weight on earth to weight on the moon. However at the end of the program I am trying to ask the user if they want to repeat the process and read their response.

If the user responds in the affirmative the script should repeat, otherwise the script should exit.

This is what I have so far, however I can't figure out how to get the script to repeat if the user decides not to quit.

echo -n "Enter starting weight: "
read star

echo -n "Enter ending weight: "
read end

echo -n "Enter weight increment: "
read increment

while [ $star -le $end ]
do
  moonweight=`echo $star \* .166 | bc`
  echo "$star pounds on earth = $moonweight pounds on the moon"

  star=`expr $star + $increment`
done

notDone=true

while [ $notDone ]
do
  echo -n "Enter a number or Q to quit: "
  read var1 junk

  var1=`echo $var1 | tr 'A-Z' 'a-z'`

  if [ $var1 = "q" ]
  then
    echo "Goodbye"
    exit
  else
  fi
done
1
  • From a Unix utility point of view, repeating the task would be a matter for the user, not the script. They would simply run the script again... Also, all of the user input that you do interactively would be better done by reading command line arguments provided by the user. Commented Oct 26, 2022 at 11:17

1 Answer 1

1

Wrapping this into another while loop:

while :
do
    echo -n "Enter starting weight: "
    read star

    echo -n "Enter ending weight: "
    read end

    echo -n "Enter weight increment: "
    read increment

    while [ "$star" -le "$end" ]
    do
      moonweight=`echo $star \* .166 | bc`
      echo "$star pounds on earth = $moonweight pounds on the moon"

      star=`expr $star + $increment`
    done

    notDone=true

    while $notDone
    do
      echo -n "Enter a number or Q to quit: "
      read var1 junk

      var1=`echo $var1 | tr 'A-Z' 'a-z'`

      if [ "$var1" = "q" ]
      then
        echo "Goodbye"
        exit
      else
        notDone=false
      fi
    done
done

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.