Shell Programming until

unti and while loops contrary, when the judgment condition is not satisfied if cyclic, once judgment condition is satisfied, on the termination of the cycle
until the scene rarely use, general use while you can

Shell until the cycle is used as follows:

until condition
do
    statements
done

condition expressed determination condition, statements express statement to be executed (can be only one, you can have multiple), do Shell and done all keywords

Execution flow until the cycle is as follows:

    First of condition to judge, if the condition is not satisfied, it enters a loop is executed until the loop statements (statements between do and done), thus completing the cycle.
    Every execution is going to be done to determine whether the condition re-established, if not established, to enter the next cycle, continue to implement the loop body statement, if established, would end until the whole cycle, execute other code after Shell's done.
    If you start condition on the establishment, then the program would not have the opportunity to enter the statements between the body of the loop, do and done there will be no execution

# Note that you must have in the body of the loop until the corresponding statement makes the condition more and more close to "set up", ultimately the only way to exit the loop, or until it becomes an infinite loop, will always execute down, never-ending

# 上节《Shell while循环》演示了如何求从 1 加到 100 的值,这节我们改用 until 循环
#!/bin/bash
i=1
sum=0
until ((i > 100))
do
    ((sum += i))
    ((i++))
done
echo "The sum is: $sum"

In the while loop, the condition is determined as ((i <= 100)), where the condition is determined to ((i> 100)), both on the contrary

##案例
cat shift.sh
#----------------------------输出文字-开始----------------------------
#!/bin/bash
until [ -z "$1" ]  # Until all parameters used up
do
  echo "$@ "
  shift
done
#----------------------------输出文字-结束----------------------------

sh shift.sh 1 2 3 4 5 6 7 8 9
#----------------------------输出文字-开始----------------------------
1 2 3 4 5 6 7 8 9
2 3 4 5 6 7 8 9
3 4 5 6 7 8 9
4 5 6 7 8 9
5 6 7 8 9
6 7 8 9
7 8 9
8 9
9
#----------------------------输出文字-结束----------------------------


 

Published 65 original articles · won praise 14 · views 1428

Guess you like

Origin blog.csdn.net/qq_41871875/article/details/104312333