Summary of loop statements in Shell (for, while,)

Source: Internet
Author: User

In programming languages, loop statements are one of the most basic syntaxes. In Shell (Bash here), there is no exception. Sort out the content you have previously written.
This includes the for/while/until loop and variable auto-increment syntax instance.

The loop statements in Shell (using Bash as an example) generally include for, while, and until. When there are occasional error syntaxes, We will summarize them with examples here. It also provides a quick way to obtain data for future use.

1. for Loop statements
Instance 1.1 Basic for loop: (traditional form, for var in ...)

View Code BASH
#!/bin/bashfor x in one two three fourdoecho number $xdone

Note: The "for" loop always receives a certain type of word list after the "in" statement. In this example, four English words are specified, but the word list can also reference files on the disk, or even file wildcards.
Instance 1.2 performs a for loop on the files in the directory.

View Code BASH
#!/bin/bashfor x in/var/log/*do#echo "$x is a file living in /var/log"echo $(basename$x) is a file living in/var/logdone

Note: $ x obtains the absolute path file name. You can use the "basename" executable program to remove the preceding path information. If you only reference files in the current working directory (for example, if you enter "for x in *"), the generated file list will have no prefix for path information.
Instance 1.3 performs a for loop on location parameters

View Code BASH
#!/bin/bashfor thing in"$@"doecho you typed ${thing}.done

Instance 1.4 for Loop uses seq to generate the number of cycles, plus the for loop statement in the C language form

View Code BASH
#!/bin/bashecho"for: Traditional form: for var in ..."for j in $(seq15)doecho$jdoneecho"for: C language form: for (( exp1; exp2; exp3 ))"for((i=1; i<=5; i++ ))doecho"i=$i"done

Note: for a fixed number of cycles, you can use the seq command to implement them without variable auto-increment. Here, the C language for loop style is quite familiar.

2. while loop statement
Instance 2.1 cyclically outputs numbers 1 to 10

View Code BASH
#!/bin/bashmyvar=1while[$myvar-le10]doecho$myvarmyvar=$(($myvar + 1))done

Note: As long as the specific condition is true, the "while" statement will be executed.

3. until loop statement
Instance 3.1 cyclically outputs numbers 1 to 10
The "Until" statement provides the opposite functionality of the "while" Statement: as long as the specific conditions are false, they are repeated. The following is an "until" loop with the same function as the previous "while" loop.

View Code BASH
#!/bin/bashmyvar=1until[$myvar-gt10]doecho$myvarmyvar=$(($myvar + 1))done

Variable auto-increment is often used when writing loops in Linux Shell. Now, let's summarize the auto-increment method of integer variables.
As I know, in bash, variable auto-increment has five methods:
1. I = 'expr $ I + 1 ';
2. let I + = 1;
3. (I ++ ));
4. I = $ [$ I + 1];
5. I = $ ($ I + 1 ))
A simple example is as follows:

View Code BASH
#!/bin/bashi=0;while[$i-lt4];doecho$i;      i=`expr$i + 1`;      # let i+=1;# ((i++));# i=$[$i+1];# i=$(( $i + 1 ))done

This article is from the following two blog posts:

Http:// /? Uid-225738-action-viewspace-itemid-227492

Http:// /? Uid-225738-action-viewspace-itemid-220988

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: 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.