Loop statement in Shell, instance of Judgment statement _linux Shell

Source: Internet
Author: User
Tags case statement

There are 3 main types of shell loops, for,while,until
The Shell's branch judgment is mainly 2 kinds, if,case

A, for loop

Copy Code code as follows:

#!/bin/bash

For file in $ (ls/tmp/test/mytest |grep SH) The//for in format is the basic format for the shell for, and the root JS for in is similar
Do//cycle start you take it as {
Echo $file
Done//Loop end you think of it as}

For ((i=0;i<10;i++))//note is double parenthesis, due to the influence of other languages, it is easy to mistake
Todo
Echo-n $i
Done

echo \//Output line Wrap

For i in 0 1 2 3 4 5 6 7 8 9
Todo
Echo-n $i
Done

echo \

For-I in "0 1 2 3 4 5 6 7 8 9"//This root is a different one, this loop only loops once, double quotes inside is just a variable
Todo
Echo-n $i
Done

Exit 0

Two, while loops

Copy Code code as follows:

#!/bin/bash
I=0
while ((I<10))
Todo
Echo $i
((i + 1))
Done

I=0
While [$i-LT 10]//Note the space on the two sides of the bracket
Todo
Echo $i
Let "i+=1"//+ 1
Done
Exit 0

Three, until cycle

Copy Code code as follows:

#!/bin/bash

End_condition=end
Until ["$var 1" = "$END _condition"]//Read the variable root set variables equal to exit the loop, otherwise forever loop
Todo
echo "Input variable #1"
echo "($END _condition to exit)"
Read Var1
echo "Variable #1 = $var 1"
Echo
Done
Exit 0

Four, if statement

Copy code code as follows:

#!/bin/bash

echo "Input a number #1"
Read num
echo "Variable #1 = $num"

If [$num-lt 60]//Note the front of Lt--it's easy to forget
Then
echo "You are not are"
elif [$num-lt] && [$num-ge 60]//judgment of multiple conditions
Then
echo "Pass"
elif [[$num-lt && $num-ge 70]]//If put together, be aware that both brackets are not written [$num-lt && $num-ge 70]
Then
echo "Good"
Elif (($num <=)) && (($num >= 85))//For people who have a language base, this kind of writing is very comfortable, don't forget the double parenthesis
Then
echo "Very good"
Else
echo "num is wrong"
Fi//if to have the end tag, the root XML is very similar, not closed, the error

Exit 0

Five, Case statement

Copy Code code as follows:

#!/bin/sh
Case is in
Start
echo "Start OK"
;; Note that a double semicolon should be noted
Stop
echo "Stop OK"
;;
Restart)
echo "Restart OK"
;;
*)
echo "No param"
;;
ESAC//Note closing label
Exit 0

[root@krlcgcms01 forif]# sh c1.sh stop
Stop OK

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.