Looping a Bash Shell Script

2019-08-07 05:59发布

问题:

I'm pretty much brand new to linux, and I've written a simple bash shell script that asks a user for a number and then asks for another number and displays the sum and product of the numbers. I have no problems with this, but I'm wanting to loop the script.

For instances, I want to ask the user if they want to quit, and if they choose not to quit the script starts over and asks for two numbers again. If there's anyone out there who knows stuff about loops, could you help me out please? Thanks.

Here's my code:

#!/bin/bash


echo -n "Name please? "
read name
echo "enter a number."
read number1
echo "enter another number"
read number2
echo "Thank you $name"
let i=0
let i=$number1+$number2
let x=0 
let x=$number1*$number2
echo "The sum of the two numbers is: $i"
echo "The product of the two numbers is: $x"
echo "Would you like to quit? Y/N? "
quit=N
while [ "$quit" = "Y" ]
do 
  clear  
  while ["$quit" != "Y" ]
  do
    echo "enter a number."
    read number1
    echo "enter another number"
    read number2
    echo "Thank you $name"
    let i=0
    let i=$number1+$number2
    let x=0 
    let x=$number1*$number2
    echo "The sum of the two numbers is: $i"
    echo "The product of the two numbers is: $x"
    echo "Would you like to quit? Y/N? "

回答1:

#!/bin/bash

# Initialize quit so we enter the outer loop first time
quit="N"

# Loop while quit is N
while [ "$quit" = "N" ]
do
  echo -n "Name please? "
  read name
  echo "enter a number."
  read number1
  echo "enter another number"
  read number2
  echo "Thank you $name"
  let i=0
  let i=$number1+$number2
  let x=0 
  let x=$number1*$number2
  echo "The sum of the two numbers is: $i"
  echo "The product of the two numbers is: $x"

#reset quit - so we enter the inner loop first time
  quit="" 

#we want to repeat until quit is Y or N: 
#while quit is not "Y" and quit is not "N"
  while [ "$quit" != "Y" -a "$quit" != "N" ]
  do
    echo "Would you like to quit? Y/N?"
    read quit

#Convert lower case y/n to Y/N
    quit=`echo $quit | tr yn YN`
  done
done


回答2:

while [[ "$(read -p "Quit?" q;echo $q)" != "y" ]] ; do
    echo okay, go on
done


回答3:

Here is a quick example of loops:

#!/bin/env bash
let quit="N"
while [ $quit != "Y" ]; do
    echo "To quit, enter Y:"
    read quit
done


回答4:

#!/bin/bash
function sumAndProd {
    read -p "enter a number: " number1
    read -p "enter another number: " number2
    echo "Thank you $name"

    sum=$((number1+number2))
    prod=$((number1*$number2))

    echo "The sum of the two numbers is: $sum"
    echo "The product of the two numbers is: $prod"
}

read -p "Name please? " name

quit=N
while [[ "$quit" != Y ]]
do 
    sumAndProd
    read -p "Would you like to quit? Y/N? " quit  
done

This is more a code-review.

  • The main thing is, to put the repeated part in a function.
  • instead of i, x, you better use expressive names, maybe abbreviated like sum and prod.
  • if you follow an assignment (let a=0) with an assignment, without using your variable before, the first assignment is senseless.
  • The name of the user will not change - we need to ask for this just once.
  • while (cond) ; do {block} done # is a form of loop.
  • You can specify a prompt (-p) with read, so this will end in one instead of two lines.
  • per se, you will not find let very often. For arithmetic expressions, x=$((expression)) is far more common.
  • Since you don't reuse sum and prod, you don't even need a variable. Just echo The sum is $((number1+number2))


回答5:

The easiest thing to do is to loop forever and break when the user quits:

while true
do
    read -p "Continue to loop? " answer
    [ "n" = "$answer" ] && break
done
echo "Now I'm here!"

The break command breaks you out of the current loop and continues right after the loop. No need for a flag variable.

By the way:

[ "n" = "$answer" ] && break

Is the same as:

if [ "n" = "$answer" ]
then
    break
fi

Note the -p for the prompt in the read command. That way, you can prompt and read the variable at the same time. You can also use \c in echo statements to suppress the New Line, or use printf which doesn't do a NL:

read -p "What do you want? " wants   #Prompt on the same line as the read

echo "What are your desires? \c"     #Prompt on the same line as the read
read desires

printf "What are your hopes? "       #Prompt on the same line as the read
read hopes

Hope this helps.



回答6:

#!/bin/sh
while true
do
 /var/www/bash/grep_ffmpeg.sh
 sleep 15
done

loop with nohup