Shell Script Programming Loop statements

Source: Internet
Author: User
Tags case statement

  • For
  • While
  • If
  • Case
  • 1. for usage 1

        for 变量 in 值1 值2 值3..;do               执行语句     done
      • Usage 2

        for 变量 `命令`;do                 #  可以引用命令执行结果        执行语句done
      • Usage 3

        for ((初始值;循环控制;变量变化));do        执行语句done   #C语言风格的for循环用法

        For loop example (1+2+3+: +100=?)

        #!/bin/bashsum=0                              # 初值为0for  ((i=1;i<=100;i++));do        sum=$(( $sum + $i))    # 初值+i 并刷新sum值doneecho "1+2+3+..+100=$sum"
      1. While loop, the number of repetitions is to use a condition to control whether to continue executing this statement, in order to avoid the dead loop, must ensure that the loop body contains the loop exit condition (exiting status exists)

        #!/bin/bashsum=0i=1           # sum i 赋初值while (( i<=100));do       #进入循环体,每循环一次判断一次i的值是否符合(( )) 的条件        let "sum+=i"        let "i+=2"doneecho "sum=$sum"
        • Unconditional loop

          while true;do    执行语句

          Done # This loop statement will never jump out, regardless of the situation while the statement is true, will continue to execute "execution statement"
          Instead, the until loop is "not executed if the condition is met" compared to the while loop.
          To 1+2+3+. +100=? As an example

          until  [ $i -gt 100];do                # 当 i 的值大于100时               sum=$(($sum+$i))    i+$(($i+1))

          Done
          echo "$sum"

    If statement, compared with case, is also conditional judgment statement, logic is relatively simple

        #!/bin/bash    read -p "Please Enter a Number:" number           # 读取终端输入的数字    if [ $number -eq 1  ];then            echo "the number is 1 "                                    # 如果输入数字等于 1 ,输出此句    elif [ $number -eq 2 ]            echo "the number is 2 "                                    # 如果输入数字等于 2 ,输出此句    elif [ $number -eq 3 ]            echo "the number is 3"                                     # 如果输入数字等于 2 ,输出此句    else            echo "the number is greater than 3 "               # 如果输入的数字都不满足,则输出此句    fi

    Case statement

        #!/bin/bash    read -p "Please Enter a Number:" number    case $number in            1) echo "the number is 1";;            2) echo "the number is 2 ";;            3) echo "the number is 3 ";;            *) echo "the number is greater than 3"    esac                     # 很容易理解,而且看起来比if 简洁一些,

    Shell Script Programming Loop statements

    Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.