zoukankan      html  css  js  c++  java
  • shell 流程控制语句

    case语句

    case $变量名 in
     "值1")
       如果变量的值等于值1,则执行程序1 ;;
      "值2")
       如果变量的值等于值2,则执行程序2 ;;
       ...省略其他分支...
       *) 
       如果变量的值都不是以上的值,则执行此程序 ;;
    esac  //case反过来写
    
    "case语句的使用实例"
    #!/bin/bash
    read "please input yes /no": cho
    case "$cho" in
     "yes")
         echo "输入正确!";;
      "no")
         echo "输入错误!";;
        *)
         echo "输入异常!";;
    esac
    ------------------------------------使用case的语句进行流程控制------------
    [root@ssgao shell]# cat case_a.sh 
    #!/bin/bash
    read op
    case $op in 
      a)
        echo "你输出的信息为 a ";;
      b) 
        echo "你输入的信息为 b ";;
      *)
        echo "输入错误!"
    esac
    [root@ssgao shell]# sh case_a.sh 
    a
    你输出的信息为 a 
    

    for语句

    for 变量 in 值1 值2 值3 ...
     do
      程序
     done
    
    -----------使用实例--------- 
     #! /bin/bash
     for i in 1 2 3 4 5
      do
        echo $i
      done
    -----------使用实例-------------
    解压缩某个目录下的压缩文件
    #!/bin/bash
    cd /root/test/
    ls *.tar.gz > ls.log
    ls *.tgz >> ls.log
    for i in $(cat ls.log)
      do 
        tar -zxf &> /dev/null
      done
    rm -rf ls.log
    

    select循环

    select 变量 in 列表
      do 
        cmd ...
      done
    
    [root@ssgao shell]# vim g.sh
    #!/bin/bash
    select a in "a" "b" "c" "d"
    do
     echo $a 
     break
    done
    [root@ssgao shell]# sh g.sh 
    1) a
    2) b
    3) c
    4) d
    #? 3 //选择第三行信息
    c
    

    until循环

    until循环,和while循环相反,until循环时只要条件判断式不成了则进行循环
    并执行循环程序。
    一旦循环条件成立,则终止循环
    ​
    until [ 条件判断式 ]
     do
      程序
     done
    
    -----------使用实例------------
    #!/bin/bash
    #从1加到100
    i=1
    s=0
    until [$i -ge 100] #如果变量i的值大于等于100,则停止执行循环
     do
      s=$(($s+$i))
      i=$(($i+1))
     done
    echo "the sum is : $s"
    

    while循环

    while循环是不定循环,也称作条件循环。主要条件判断式成立,循环就会一直继续,直到条件判断式不成立,循环才会停止。
    这和for的固定循环不太一样
    while [ 条件判断式 ]
     do
      程序
     done
    
      continue ; //继续执行
      break; //跳出循环 
    
    ------------------使用实例--------------
    #!/bin/bash
    i=1
    sum=0
    while [ $i -le 100 ]
     do
      sum=`expr $sum + $i`
      i=`expr $i + 1`
     done
    echo "the sum is : $sum"
         
    [root@ssgao shell]# sh while.sh 
    the sum is : 5050     
         
        
        
    [root@ssgao shell]# cat while.sh 
    #!/bin/bash
    i=0
    while [ $i -le 100 ]
     do
      i=`expr $i + 1 `
      if [ $i -eq 10 ]
        then
          echo "contine" 
          continue;   //-----体会continue的使用
      elif [ $i -eq 11 ]
        then
         echo "break"
         echo "${i}"
        break; //----体会break的使用
      fi
     done    
    [root@ssgao shell]# sh while.sh  //运行查看执行的结果
    contine
    break
    11 
    
  • 相关阅读:
    AGC037F Counting of Subarrays
    AGC025F Addition and Andition
    CF506C Mr. Kitayuta vs. Bamboos
    AGC032D Rotation Sort
    ARC101F Robots and Exits
    AGC032E Modulo Pairing
    CF559E Gerald and Path
    CF685C Optimal Point
    聊聊Mysql索引和redis跳表
    什么是线程安全
  • 原文地址:https://www.cnblogs.com/ssgao/p/8869696.html
Copyright © 2011-2022 走看看