【问题标题】:Looping a Bash Shell Script循环 Bash Shell 脚本
【发布时间】:2011-04-12 20:33:09
【问题描述】:

我对 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? "

【问题讨论】:

  • 投票关闭它,因为它是面向外壳的,可能更适合 Superuser.com
  • 来吧,bash 编程比 php 少吗? ;-)

标签: linux bash shell loops


【解决方案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

【讨论】:

  • @rjdelight:请将此答案标记为已接受...不这样做是不礼貌的,并且忘记了 Erik 为获得这个答案所花费的时间!
【解决方案2】:
while [[ "$(read -p "Quit?" q;echo $q)" != "y" ]] ; do
    echo okay, go on
done

【讨论】:

    【解决方案3】:

    这是一个循环的简单示例:

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

      这更像是一次代码审查。

      • 主要是将重复的部分放在一个函数中。
      • 您最好使用富有表现力的名称,而不是 i、x,可以缩写为 sum 和 prod。
      • 如果你在一个赋值(let a=0) 后面加上一个赋值,而之前没有使用你的变量,那么第一个赋值是没有意义的。
      • 用户名不会改变 - 我们只需要请求一次。
      • while (cond) ; do {block} done# 是循环的一种形式。
      • 您可以使用 read 指定一个提示符 (-p),因此它将以一行而不是两行结束。
      • 就其本身而言,您不会经常找到let。对于算术表达式,x=$((expression)) 更为常见。
      • 由于您不重复使用 sum 和 prod,因此您甚至不需要变量。只需echo The sum is $((number1+number2))

      【讨论】:

        【解决方案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 命令中的提示。这样,您可以同时提示和读取变量。您还可以在 echo 语句中使用 \c 来抑制换行,或者使用不执行 NLprintf

        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
        

        希望这会有所帮助。

        【讨论】:

          【解决方案6】:
          #!/bin/sh
          while true
          do
           /var/www/bash/grep_ffmpeg.sh
           sleep 15
          done
          

          使用 nohup 循环

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2019-10-03
            • 2017-11-13
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-06-28
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多