Shell Scripting Learning notes-looping structure

Source: Internet
Author: User

1. For loop statements

1.1. Syntax Format:

For Var in listdo    Commandsdone
for ((expression)) do     Commandsdone

1.2 Simple example:

#!/bin/sh# read echo from the direct list "read from direct list" for Var in 1 2 3do  echo "$var" Doneecho "\ n" #从变量读取echo "read from variable" list= "a b C" for Var in $li Stdo  echo "$var" Doneecho "\ n" #从命令读取echo "command read" for Var in ' cat Test.txt ' do   echo ' $var "Doneecho" \ n "#从目录读取echo" Reads "for Var in ~/desktop/hello*do   echo" $var "done#for+ expression structure from the directory Sum=0echo" for+ expression Structure "for ((i=0;i<=10;i++)) do   ((sum+=i)) Doneecho "sum is: $sum" echo "\ n"
Operation Result:


1.3 Separators

1.3.1 Default delimiter: Spaces, tabs, line breaks

1..3.2 modify delimiter, e.g. set semicolon ";" to delimiter: ifs=$ ";"


Note: The delimiter setting is not valid for the direct list because it is a delimited list for the direct list.

2. While and until statements

The 2.1 while statement, which indicates that a loop is executed when the condition is satisfied, and the statement structure is as follows:

While Test Commanddo    Commandsdone
Until Test Commanddo   Commandsdone
2.2 Simple Example

#!/bin/sh#while structure echo "While loop structure" Var=0while [$var-lt 3]do  var=$[$var + 1]  echo "$var" Doneecho "\ n" #until结构ech o "Until cycle structure" Var=0until [$var-eq 3]do  var=$[$var + 1]  echo "$var" Doneecho "\ n"
Operation Result:


3. Nesting and control of loops

3.1 The multiplication table is output by the loop nesting mode, e.g:

#!/bin/shfor ((i=1; i<=9; i++)) do for        ((j=1; j<=i; j + +)) do                printf '%dx%d=%-3d ' $i $j $[$i * $j]        D One        Echodone
Operation Result:


3.2 Through the break statement and continue statement can be loop control, break statement to jump out of the loop, continue the function of the statement out of the current loop, continue the next loop, e.g:

#!/bin/sh#break statement, jump out of loop echo "break statement, jump out of Loop" for ((I=1; i<=5; i++)) does   if [$i-eq 3] Then break   fi   echo "$i" doneecho#continue statement, jump out of the current loop, continue the Next loop echo "continue statement, jump out of the current loop, continue to the next loop" for ((I=1; i<=5; i++)) do   if [$i-eq 3]   then     continue   fi   echo "$i" done
Operation Result:


Shell Scripting Learning notes-looping structure

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.