Bash Shell Script Loop

I'm pretty much new to linux, and I wrote a simple bash script that asks the user for a number and then asks for a different number and displays the sum and product of numbers. I have no problem with this, but I want a loop script.

For instances, I want to ask the user if they want to exit, and if they don’t want to stop running the script and ask for two numbers again. If there is anyone there who knows anything about loops, could you help me? Thank you

Here is 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
source share
6 answers
 #!/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 
+3
source
 while [[ "$(read -p "Quit?" q;echo $q)" != "y" ]] ; do echo okay, go on done 
+2
source

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 
0
source

The easiest thing to do is loop forever and break when a user exits:

 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 immediately after the loop. There is no need for a flag variable.

By the way:

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

Same as:

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

Note -p for a prompt in the read command. This way you can query and read the variable at the same time. You can also use \c in echo statements to suppress a new line, or use printf , which does not execute 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.

0
source
 #!/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 of a code review.

  • The main thing is to put the repeating part in the function.
  • instead of i, x, it is better to use expressive names, possibly abbreviated as sum and prod.
  • if you perform a task (let a = 0) with an assignment, without using your variable before, the first assignment is meaningless.
  • The username will not change - we need to ask about it only once.
  • while (cond) ; do {block} done while (cond) ; do {block} done # is a loop form.
  • You can specify a prompt (-p) with a read so that it ends in one, not two lines.
  • per se, you will not find let very often. For arithmetic expressions, x = $ ((expression)) is much more common.
  • Since you are not reusing sum and prod, you do not even need a variable. Just echo The sum is $((number1+number2))
0
source
 #!/bin/sh while true do /var/www/bash/grep_ffmpeg.sh sleep 15 done 

loop with nohup

0
source

Source: https://habr.com/ru/post/1399654/


All Articles