循环与结构化命令

     最近正在看LinuxShell编程从初学到精通这本书,写的很详细,每一章节后面都有对应的习题,自己也亲手写了下,还有一部分没有写出来,不过正在努力。学习东西要学无止境,循序渐进,希望大家帮助优化下,或者给出更好的建议,谢谢支持!

1、使用for 循环计算100以内所有偶数的和,然后用while循环和until循环来实现这个计算,比较哪种结构更简单;

for:

 

#!/bin/bash

#

#In addition to sum assigned value

sum=0

#Use the list for circulation calculated from 1 to 100 of all the sum of, and the value stored in sum

for i in {0..100..2}

do

#     echo $i

     let "sum+=i"

#     echo $sum

done

echo "sum=$sum"

 

while:

 

 

#!/bin/bash

#

#In addition to sum assigned value

sum=0

i=0

#The use of counter control while circulation calculated from 1 to 100 of all the sum of, and the value stored in sum

while (( i <= 100 ))

do

     let "sum+=i"

     echo "sum=$sum"

     let "i+=2"

     echo "i=$i"

done

echo "sum=$sum"

 

until:


 

#!/bin/bash

#

#In addition to sum assigned value

sum=0

i=0

#The use of counter control while circulation calculated from 1 to 100 of all the sum of, and the value stored in sum

until (( i > 100 ))

do

     let "sum+=i"

     echo "sum=$sum"

     let "i+=2"

     echo "i=$i"

done

echo "sum=$sum"

 

2、使用while循环或者until循环实现从命令行诗篇字符串,直到输入的字符串为句号为止; 

while:

 

#!/bin/bash

#Prompt the user to enter a string

echo "Please input a string:"

read string

#The initial Chinese flag value

signal=0

#

while [[ "$signal" != 1 ]]

do

    if [ "$string" != \. ]; then

        echo "Not, Please try again!"

        read string

    else

        signal=1

        echo "Congratulation, you are right!"

    fi

done

 

 

until:


#!/bin/bash

#Prompt the user to enter a string

echo "Please input a string:"

read string

#The initial Chinese flag value

signal=0

#

until [[ "$signal" = 1 ]]

do

    if [ "$string" != \. ]; then

        echo "Not, Please try again!"

        read string

    else

        signal=1

        echo "Congratulation, you are right!"

    fi

done

 

 

3、将for_exam9.sh中输入的命令行参数改为1 2,3,查看输出结果是否与原来相同; 

for_exam9.sh:

#!/bin/bash

#Prompts the user to input the number of parameters

echo "Number of arguments is $#:"

#Prompts the user to input

echo "What you input is:"

 

for argument

do

     echo "$argument"

done

 

输出为:

[root@Shell example]# ./3.sh '1 2' 3

Number of arguments is 2:

What you input is:

1 2

3

 

4、使用for循环实现2,4,8,16,32,64的结果显示,然后使用whileuntiil实现

while:

 

#!/bin/bash

#On sum assignment

sum=0

#To counter I Fu in addition to value

i=1

#

while (( sum < 70  ))

do

    let "sum=i*2"

    let "i=sum"

    if [ "$sum" -gt 64 ]

    then

        break

    fi

    echo "$sum"

done

 

until:

 

#!/bin/bash

#On sum assignment

sum=0

#To counter I Fu in addition to value

i=1

#

until (( sum > 70  ))

do

    let "sum=i*2"

    let "i=sum"

    if [ "$sum" -gt 64 ]

    then

        break

    fi

    echo "$sum"

done

 

5、使用循环实现从1开始叠加,直到和的结果大于2000时为止。(提示:可以通过两种方式实现,一种方式在循环条件中设置和大于2000时结束,第二种方式使用break控制符实现)

one:

#!/bin/bash

#Environment variables

int=1

# while

while (( int <= 2000 ))

do

    echo "$int"

    let "int++"

done

 

two:

 

#!/bin/bash

#Environment variables

sum=1

signal=0

# while

while [[ "$signal" -eq 0 ]]

do

    let "sum++"

    if [ "$sum" -gt 2000 ]; then

       break

    fi

    echo "$sum"

done

 

6、找出100以内能被3整除的数,每行显示8个数,然后换行显示;

#!/bin/bash

m=1

 

for (( i=1; i<100; i++ ))

do

     let "temp1=i%3"

     if [ "$temp1" -ne 0 ]

     then

           continue

     fi

     echo -n "$i"

     let "temp2=m%8"

     if [ "$temp2" -eq 0 ]

     then

         echo " "

     fi

     let "m++"

done

 

7、打印由如下组成的图案;

8、如果一个整数各位数字之和可以被9整除,那么 该整数就可被9整除,编写一个脚本提示用户输入一个整数,然后输出该整数,并告之该整数是否可被9整除;

#!/bin/bash

echo "Please input a number:"

read number

echo -n $number

#

a=$(echo $number | cut -c1 )

b=$(echo $number | cut -c2 )

c=$(echo $number | cut -c3 )

#

#list=$a+$b+$c

if [ -n "$a" ]; then

    list=$a

    if [ -n "$b" ]; then

        list=`expr "$a" + "$b"`

            if [ -n "$c" ]; then

               list=`expr "$a" + "$b" + "$c"`

            fi

    fi

else

    echo "list = ?"

fi

 

echo "list=$list"

 

let "temp=list%9"

if [ "$temp" = 0 ]

then

    echo " Can be divisible by 9."

fi

 

 

9、编写一个脚本,该脚本提示用户输入一些整数,然后通过程序控制分别计算出这些整数中奇数之和与偶数之和,并将其输出;

 (我现在做到的是只能判断奇数或偶数)

 

#!/bin/bash

#

echo "Please input some numbers:"

read num

for var in $num

do

    echo $var

    if [ `expr "$var" % 2` == 0 ]; then

        echo "$var is even"

    else

        echo "$var is odd"

  fi

     
 10、编写脚本提示用户输入一个正整数,程序将输出信息提示该正整数是否为质数,(提示只有2是质数,如果为奇数,且不能被任何一个小于或者等于它平方根的奇数整除,那么该奇数就是质数。)

11、一个数恰好等于它的因子之和,这样的数称为“完数”。例如,6的因子为123,而6=1+2+3,因此,6是“完数”。编写程序写出1000以内所有的完数,并按下面的格式 输出其因子:

6 its factors are 1 2 3

 

12、编写一个脚本读入一些整数,分别输入 出这些整数中的奇数的个数和偶数的个数,并输出0的个数;

13、编写一个脚本提示用户输入一个整数,程序将分别输出该整数第个位上的数字,并输出这些数字的和,例如,输出整数2345第个位上的2345;输出-3456第个位上的数字是3456

14、使用while循环编写脚本,使其他完成下面的功能;

1)提示用户输入两个整数:firstNumsecondNumfirstNum的值一定要小于secondNum)。

2)输出所有介于firstNumsecondNum之间的奇数。

3)输出所有介于firstNumsecondNum之间的偶数之和。

#!/bin/bash
#
echo "Please input two number:(The first number is greater than the second number)"
read firstNum secondNum
nums=`echo $firstNum $secondNum | wc -w`

signal=0

if [ -n $firstNum ]; then
   echo "$firstNum"
else
   echo "Please input firstNum:"
   read firstNum
fi
if [ -n "$secondNum" ]; then
   echo "$secondNum"
else
   echo "Please input secondNum:"
   read secondNum
fi



while (( signal != 1 ))

do
    if [ "$nums" != 2 ]; then

          echo "Please input two number:"

          read firstNum secondNum

          if [ "$firstNum" -lt "$secondNum" ]; then
              echo "Please input two number:(The first number is greater than the second number"
              read firstNum secondNum
          else
              signal=1
              echo "$firstNum" is greater than "$secondNum"
          fi
    else
          signal=1
          echo "$firstNum" is greater than "$secondNum"
    fi

done

 

15、分别使用until循环和for循环改写习题14

16、将习题7的图案的脚本加入breakcontinue循环控制符,查询显示的结果?

17、上机运行下面的脚本,查看脚本的输出结果是什么?