Linux Shell Basic Learning--cycle and condition judgment

Source: Internet
Author: User

For loop

1. Note Do must wrap

For i in {1..10} do echo $idone

2. Note Do must wrap

For I in 1 2 3 4 5 do echo $idone

3. Traverse the results of the command output

For shname in ' ls *.sh ' do echo $shnamedone

4. For loops similar to high-level languages

For ((i=1;i<100;i++)) do echo $idone

The For loop is here, and these are enough.

While loop

Min=1max=100while [$min-le $max]do echo $min min= ' expr $min + 1 ' done

[] In the conditional expression,-le is less than equals, more can refer to the comparison operator

Until cycle

Min=0max=100until [$min-ge $max]do min= ' expr $min + 1 ' echo $mindone



If judgment, note the space

1. If

value=1if [$value-eq 1]; Then echo "true" fi

2. If...else

value=80if [$value-lt];then echo "fail" else echo "Success" fi

3. If...elif...else

value=80if [$value-lt];then echo "fail" elif [$value-ge];then echo "good" else echo "Success" fi

4. && | | Operation

value=80if [$value-ge 0] && [$value-le];then echo "ok" fi

Arithmetic expressions (for add, subtract, multiply, divide, take-up)

Expr expression

i=1i= ' expr $i + 1 ' echo $ii =$ (expr $i-1) echo $i

Let expression

I=10let i= $i +1echo $ilet i*=2echo $i

Using declare

Declare-i numnum= $num +1echo $num

$ ((expression))

i=2i=$ ((i+1)) echo $i



Linux Shell Basic Learning--cycle and condition judgment

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.