繁体   English   中英

循环Bash Shell脚本

[英]Looping a Bash Shell Script

我是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? "
#!/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
while [[ "$(read -p "Quit?" q;echo $q)" != "y" ]] ; do
    echo okay, go on
done
#!/bin/sh
while true
do
 /var/www/bash/grep_ffmpeg.sh
 sleep 15
done

与nohup循环

这是循环的简单示例:

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

最简单的方法是永远循环并在用户退出时中断:

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来禁止换行,或者使用不执行NL的 printf

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

希望这可以帮助。

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

这更多是代码审查。

  • 最主要的是,将重复的部分放入函数中。
  • 最好不要使用表示性名称,而最好使用诸如sum和prod之类的缩写。
  • 如果您在赋值后面跟随一个赋值(让a = 0),而之前没有使用变量,则第一个赋值是没有意义的。
  • 用户名不会更改-我们只需要询问一次。
  • while (cond) ; do {block} done while (cond) ; do {block} done #是循环的一种形式。
  • 您可以指定一个带有读取的提示(-p),因此该提示将以一行而不是两行结尾。
  • 就其本身而言,您不会经常发现let 对于算术表达式,x = $((expression))更为常见。
  • 由于您不重用sum和prod,因此甚至不需要变量。 只是echo The sum is $((number1+number2))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM