zoukankan      html  css  js  c++  java
  • if case for while

    #!/bin/bash
    a=$1
    if [ $a ] #判断$1是否为空
    then #非空
    echo "the input is No:$a"
    exit 0
    else #空
    read -p "input a Nov:" a
    #a=$b
    fi

     


    case $a in
    "1")
    echo "your input is No:1"
    ;;

    "2")
    echo "your input is No:2"
    ;;

    *)
    echo "dont know"
    ;;
    esac

    ~~~~~~~~~~~~~~~~

    #!/bin/bash  
    ip=(
    a
    b
    c
    d
    ) #ip=("aa" "bb" "cc") 也一样

    for i in ${ip[*]} ; do   #注意不是ip[$*]
    echo $i
    done
    for ((i=0;i<10;i++));do
    echo -n $i
    done

    #!/bin/bash
    name=("Tom" "Tomy" "John")
    for i in 0 1 2
    do
    echo $i:${name[$i]}  # name[$*]   也是可以的 注意不是name[*]

    done

     

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~

    while true;do   #while 死循环

    echo “just test”

    done

    #!/bin/bash
    while [ $# -gt 0 ];do    #当$#大于0就执行          until与while相反       until [ $# -lt 1 ];do   执行,直到 $#小于1时 停止
    echo $*
    shift
    done

    结果

    zzx@ubuntu:~$ sh shift.sh 1 2 3 4 5
    1 2 3 4 5
    2 3 4 5
    3 4 5
    4 5
    5

    while true ; do           ###  shell没do while 相当于 do……while   循环

    if [];then

    echo "  test"

    break     ####break 退出while循环  继续执行剩下的脚本    如果是exit就退出整个shell  不在执行剩下脚本

    fi

    done

     
    1. while expression
    2. do
    3. command
    4. command
    5. ```
    6. done
    1、计数器控制的while循环
       主要用于已经准确知道要输入的数据和字符串的数目。
       举例
    1. 1 #!/bin/sh
    2. 2 int=1
    3. 3 while(( $int<=5 ))
    4. 4 do
    5. 5 echo $int
    6. 6 let "int++"
    7. 7 done
    2、结束标记控制的while循环
          主要用于不知道读入数据的个数,但是可以设置一个特殊的数据值来结束循环,该特殊值称为结束标    记,通过提示用户输入进行操作。
    举例
    1. 1 #用脚本演示使用结束标记控制while循环实现猜1~10内的数
    2. 2 #!/bin/sh
    3. 3
    4. 4 echo "Please input the num (1~~10): "
    5. 5 read num
    6. 6 while [[ $num != 4 ]]
    7. 7 do
    8. 8 if [ $num -lt 4 ]
    9. 9 then
    10. 10 echo "Too small ,Try again.."
    11. 11 read num
    12. 12 elif [ $num -gt 4 ]
    13. 13 then
    14. 14 echo "Too big ,Try again.. "
    15. 15 read num
    16. 16 else
    17. 17 exit 0
    18. 18 fi
    19. 19 done
    20. 20 echo "Yes ,you are right !!"
    3、标致控制的while循环
       用户输入标志值来控制循环结束
     举例
     
    1. 1 #!/bin/sh
    2. 2 echo "Please input the num:"
    3. 3 read num
    4. 4 sum=0
    5. 5 i=1
    6. 6 signal=0
    7. 7 while [[ $signal != 1 ]]
    8. 8 do
    9. 9 if [ $i -eq $num ]
    10. 10 then
    11. 11 let "signal=1"
    12. 12 let "sum+=i"
    13. 13 echo "1+2、、、+$num=$sum"
    14. 14 else
    15. 15 let "sum=sum+i"
    16. 16 let "i++"
    17. 17 fi
    18. 18 done
    4、命令行控制的while循环
      举例
    1. 1 #!/bin/sh
    2. 2
    3. 3 echo "Please input arguements is $# "
    4. 4 echo "What you input : "
    5. 5 while [[ $* != "" ]]
    6. 6 do
    7. 7 echo $1
    8. 8 shift
    9. 9 done
  • 相关阅读:
    Junit研究心得——准备研究Junit
    Nginx proxying for Tomcat applications
    将工厂模式升华为插件式框架【文摘】
    JIRA中输入验证时,将验证错误“InvalidInputException”写到对应字段顶部,而不是页面顶部。
    Junit研究心得——Junit中的Annotation
    Junit研究心得——Junit中的约定规则
    Java 编程的动态性【文摘】
    插件式设计开发心得(一)【文摘】
    Java SE 6 新特性: Instrumentation 新功能
    php实现首页自动选择语言跳转
  • 原文地址:https://www.cnblogs.com/hanxing/p/4057345.html
Copyright © 2011-2022 走看看