循环播放的Bash shell脚本(Looping a Bash Shell Script)

2019-10-16 13:17发布

我几乎全新到Linux,我已经写了一个简单的bash shell脚本,询问了一些用户,然后请求另一个号码并显示数字的总和和产品。 我有这个没有问题,但我想循环脚本。

对于情况下,我想询问用户是否要退出,如果他们选择不退出脚本从头开始,再次对两个数字要求。 如果有任何人在那里谁知道环路的东西,你能帮助我吗? 谢谢。

这里是我的代码:

#!/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? "

Answer 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


Answer 2:

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


Answer 3:

这里是循环的快速例如:

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


Answer 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

这更是一个代码审查。

  • 最主要的是,把重复的部分功能。
  • 而不是我X,你更好的使用表现力的名字,也许像简写之和督促。
  • 如果按照分配(让= 0)与分配,而不使用之前您的变量,第一个任务是毫无意义的。
  • 用户的名称不会改变 - 我们需要问这个只有一次。
  • while (cond) ; do {block} done while (cond) ; do {block} done #是循环的一种形式。
  • 您可以指定一个提示(-p)与读取,因此这将一端的两条线来代替。
  • 本身,你不会找到let非常频繁。 对于算术表达式中,x = $((表达式))是更为常见。
  • 既然你不重用之和督促,你甚至不需要一个变量。 只是echo The sum is $((number1+number2))


Answer 5:

最容易做的事情是永远循环下去,而当用户退出突破:

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

break的命令打破你出电流回路和循环之后继续。 无需一个标志变量

顺便说说:

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

是相同的:

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

注意-p在提示read命令。 这样一来,你可以提示,并在同一时间读取该变量。 您还可以使用\c在echo语句来抑制新线,或使用printf不做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

希望这可以帮助。



Answer 6:

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

循环使用的nohup



文章来源: Looping a Bash Shell Script