Linux while loop and for loop code example introduction, linuxwhile

Source: Internet
Author: User

Linux while loop and for loop code example introduction, linuxwhile

There are three types of loops in the script, which can be divided into two types: while LOOP and for loop.

1. while Loop

The while loop can be divided into two types: whiel do done and until do done.

While [condition] # There are spaces on both sides of condition do # loop start section done

When condion meets the conditions, it means that the actual cycle continues and jumps out when the conditions are not met.

Until [condition] do program segment done

The until do done method is the opposite of the while method. When the condition is set, it jumps out of the loop; otherwise, it continues.

#!/bin/bash#program:#       This program shows "Hello World!" in your screen#History:#205/08/3 rhx First ReleasePATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/binexport PATHs=0i=1while [ "$i" != "100" ]do     i=$(($i+1))s=$(($s+$i))doneecho "The result of '1+2+3+...+100' is --> $s"

Note: For numeric calculation, var =$ (Operation content ))

Sum of the first 100 counts:

2. for Loop
For var in con1 con2 con3... do program segment done
#!/bin/bash#program:#       This program shows "Hello World!" in your screen#History:#205/08/3 rhx First ReleasePATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/binexport PATHs=0i=0for i in $(seq 1 100)do     i=$(($i+1))s=$(($s+$i))doneecho "The result of '1+2+3+...+100' is --> $s"

Note: (seq 1 100) indicates the sequence.

For Loop processing of numerical values

The left and right sides of the inner brackets do not require spaces, but try to add spaces on the left and right sides when writing the script.

For (initialvalue; limitvalue; step) do program segment done
#!/bin/bash#program:#       This program shows "Hello World!" in your screen#History:#205/08/3 rhx First ReleasePATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/binexport PATHnu=100i=0for (( i=1;i<=$nu;i=i+1))do s=$(($s+$i))doneecho "The result of '1+2+3+...+100' is --> $s"

3. shell tracking and debugging

Before executing shell, the most worrying thing is the syntax error. How can I debug it?

[root @loacalhost ~]#sh [-nvx] scripts.sh

Parameter Parsing:

-N: Check the syntax without executing the script.

-V outputs the script content to the screen before executing the script.

-X displays the script content used on the screen. This is a common parameter.

[root @loacalhost ~]#sh [-nvx] scripts.sh

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.