Shell Script--Grammar Chapter

Source: Internet
Author: User

Condition test

1. Test condition tests or [] (test conditions are true false)

Example: string comparison

#!/bin/bash
Read data
Test "$data" = = "Hello" //note there are spaces on either side of the equal sign or [$data = = "Hello"]
echo $? //Run result of previous command, success is 0, failure is 1


650) this.width=650; "title=" QQ picture 20160810202257.png "Src=" http://s4.51cto.com/wyfs02/M00/85/A7/ Wkiol1erhr-x9mkjaaarfnpmpza433.png "alt=" Wkiol1erhr-x9mkjaaarfnpmpza433.png "/>


Example: two number comparison size

#!/bin/bash
data=200
[$data-eq 100]
echo $?
[$data-LT 100]
echo $?
[$data-le 100]
echo $?
[$data-GT 100]
echo $?
[$data-ge 100]
echo $?


650) this.width=650; "title=" QQ picture 20160810204114.png "Src=" http://s4.51cto.com/wyfs02/M02/85/A7/ Wkiol1eriodxbmfqaaaizovh3ks109.png "alt=" Wkiol1eriodxbmfqaaaizovh3ks109.png "/>


650) this.width=650; "width=" 384 "height=" "title=" QQ picture 20160810205512.png "style=" width:351px;height:220px; "src= "Http://s5.51cto.com/wyfs02/M02/85/A7/wKioL1erI5LBwmBYAADtOBsIJ0w427.png" alt= " Wkiol1eri5lbwmbyaadtobsij0w427.png "/>


650) this.width=650; "Width=" 609 "height=" 146 "title=" QQ picture 20160810205711.png "style=" WIDTH:530PX;HEIGHT:147PX; "src= "Http://s2.51cto.com/wyfs02/M02/85/A8/wKiom1erJHrg86p2AADIymt-B98039.png" alt= " Wkiom1erjhrg86p2aadiymt-b98039.png "/>

For example: Test and, non-logical operations

650) this.width=650; "Width=" 522 "height=" "title=" QQ picture 20160810211610.png "style=" width:518px;height:330px; "src= "Http://s3.51cto.com/wyfs02/M00/85/A8/wKiom1erKMzTXNOMAADCx0Y4Cxc703.png" alt= " Wkiom1erkmztxnomaadcx0y4cxc703.png "/>

2, If/then/elif/else/fi

#!/bin/bash
Read data
If ["$data"-eq];then
echo "100"
elif ["$data"-eq];then
echo "200"
Else
: //: Empty command, the command does nothing
Fi

650) this.width=650; "title=" QQ picture 20160810213411.png "style=" Float:none; "src=" http://s2.51cto.com/wyfs02/M02/85/A7 /wkiol1erllgjd0fmaaauk_mqqu8914.png "alt=" Wkiol1erllgjd0fmaaauk_mqqu8914.png "/>


#!/bin/bash
Read data
["$data"-eq] && { //Set execution curly braces inside, otherwise execute brackets outside

echo "Yes"
Exit 0
}
echo "No"

650) this.width=650; "title=" QQ picture 20160810214023.png "style=" Float:none; "src=" http://s2.51cto.com/wyfs02/M00/85/A8 /wkiom1erllgxqgylaaaouuykt9u657.png "alt=" Wkiom1erllgxqgylaaaouuykt9u657.png "/>


#!/bin/bash
Read data
["$data"-eq 100] | | { //Do not set execution curly braces inside, otherwise execute brackets outside

echo "Yes"
Exit 0
}
echo "No"

650) this.width=650; "title=" QQ picture 20160810214551.png "Src=" http://s2.51cto.com/wyfs02/M01/85/A8/ Wkiom1erl3oiqm-saaapefc0lzc179.png "alt=" Wkiom1erl3oiqm-saaapefc0lzc179.png "/>


3, Case/esac

#!/bin/bash
Read Val
Case ' $val ' in
Start | -S | -start | -start)
echo "Start"
;;
' Stop ')
echo "Stop"
;;
‘*‘ )
echo "*"
;;
Esac

650) this.width=650; "title=" QQ picture 20160810220302.png "Src=" http://s5.51cto.com/wyfs02/M00/85/A7/ Wkiol1erm7piv8gjaaapzhkpyko362.png "alt=" Wkiol1erm7piv8gjaaapzhkpyko362.png "/>


Looping statements

1, For/do/done

Writing one:

#!/bin/bash

For i in {1..100}

For I in {A.. Z
Do
echo "$i"
Done

Run Result: print a...z letters


Two:

#!/bin/bash
For ((i=0;i<=100;i++))
Do
echo "$i"
Done

Run Result: Print 1 ... 100 of the numbers

2, While/do/done && Until/do/done

650) this.width=650; "Width=" 614 "height=" "title=" QQ picture 20160810233640.png "Style=" width:610px;height:181px;float : none; "src=" Http://s2.51cto.com/wyfs02/M02/85/A9/wKiom1erSk2Bzg77AAATie7TUzw879.png "alt=" Wkiom1ersk2bzg77aaatie7tuzw879.png "/>


650) this.width=650; "Width=" 503 "height=" 164 "title=" QQ picture 20160810233955.png "Style=" width:611px;height:192px;float : none; "src=" Http://s4.51cto.com/wyfs02/M02/85/A9/wKiom1erSk2Aip4aAAASBYk-sWU286.png "alt=" Wkiom1ersk2aip4aaaasbyk-swu286.png "/>


To find the even and the 1~100 between

#!/bin/bash
Sum=0
Ret=0
For i in {1..100}
Do
Let ret=i%2
If [$ret-eq 0];then
Let Sum+=i
Fi
Done
Echo $sum

Run Result: 2550

3. How to debug shell scripts

Shell Script--Grammar Chapter

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.