Linux shell scripts use structured commands (2)

Source: Internet
Author: User

A. For command

Two. While command

Three. Until command

1.for Command Basic format

1  for inch List 2  Do 3     Commands4 done
1[Email protected]:~/testshell>CatFortest.SH 2#!/bin/Bash3#test forCommand4 5  forCityinchBeijing Shanghai Shenzhen Dalian6  Do7     EchoThe city is $city8  Done    9[Email protected]:~/testshell>./fortest.SH Ten The city is Beijing One The city is Shanghai A The city is Shenzhen -The city is Dalian

A C language-style for command

1  for ((variable assignment; condition; iterationprocess)) 2  Do 3      Commands4 done
1[Email protected]:~/testshell>CatFortest.SH 2#!/bin/Bash3#test forCommand4 5 sum=06  for((i=1; i<= -; i++ ))7  Do8((sum=sum+i))9 Ten  Done One Echo sum= $sum A  -  for((a=1, b=1;a<5,b<3; a++,b++ )) -  Do the((c = A +b)) -     Echoc =$c -  Done -[Email protected]:~/testshell>./fortest.SH  + sum=5050 -c =2 +c =4

2.while Command Basic format

1  while Test Command 2  Do 3 Other     commands4 done
1[Email protected]:~/testshell>CatWhiletest.SH 2#!/bin/Bash3#test whileCommand4 5Var=36 7  while[$var-GT0 ]8  Do9(var = var-1 ))Ten     Echovar =$var One  Done A[Email protected]:~/testshell>./whiletest.SH  -var =2 -var =1 thevar =0

3.until Command Basic format

1 until Test Commands 2  Do 3 Other     commands4 done
1[Email protected]:~/testshell>CatUntiltest.SH 2#!/bin/Bash3#testuntilCommand4 5Var=56 7 until[$var-GT8 ]8  Do9((var++ ))Ten     Echovar =$var One  Done A[Email protected]:~/testshell>./untiltest.SH  -var =6 -var =7 thevar =8 -var =9

Another thing is that the loop output can be output to the screen or output to a file, which is to add a processing command after the done command

1[Email protected]:~/testshell>CatUntiltest.SH 2#!/bin/Bash3#testuntilCommand4 5Var=56 7 until[$var-GT8 ]8  Do9((var++ ))Ten     Echovar =$var One  Done>Result.txt A[Email protected]:~/testshell>./untiltest.SH  -[Email protected]:~/testshell>ls -Untiltest.SHResult.txt the[Email protected]:~/testshell>CatResult.txt -var =6 -var =7 -var =8 +var =9

Linux shell scripts use structured commands (2)

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.