2018-05-31 Linux Learning

Source: Internet
Author: User

20.10 for Loop

Syntax: for variable name in condition; Do ...; Done

Case 1
#!/bin/bash
Sum=0
For I inseq 1 100
Do
echo "$sum + $i"
sum=$[$sum + $i]
Echo $sum
Done
Echo $sum

Case 2
File list loop
#!/bin/bash
cd/etc/
For a in ls/etc/
Do
If [-D $a]
Then
LS $a
Fi
Done

Operation Process

Case 1

[Email protected] shell]# vim for1.sh
#!/bin/bash
Sum=0
For I inseq 1 100
Do
sum=$[$sum + $i]
Done
Echo $sum

[[email protected] shell]# sh for1.sh 5050

Case 2

[Email protected] shell]# vim for2.sh
#!/bin/bash
cd/etc/
For a in ls/etc/
Do
[-D $a] && ls $a
Done

20.11 While loop

syntax while condition; Do ...; Done
Case 1
#!/bin/bash
While:
Do
load= w|head-1|awk-f ' Load average: ' {print $} ' |cut-d. -f1
If [$load-GT 10]
Then
Top|mail-s "Load is High: $load" [email protected]
Fi
Sleep 30
Done

Case 2
#!/bin/bash
While:
Do
Read-p "Please input a number:" N
If [-Z "$n"]
Then
echo "You need input sth."
Continue
Fi
n1=echo $n|sed ‘s/[0-9]//g‘
If [-N "$n 1"]
Then
echo "You just only input numbers."
Continue
Fi
Break
Done
Echo $n

Operation Process

Case 1

[[email protected] shell]# uptime|awk -F ‘load average:‘ ‘{print $2}‘ 0.00, 0.01, 0.05[[email protected] shell]# uptime|awk -F ‘load average:‘ ‘{print $2}‘|cut -d . -f1 0[[email protected] shell]# uptime|awk -F ‘load average:‘ ‘{print $2}‘|cut -d . -f1|sed ‘s/ //‘0

[Email protected] shell]# vim while1.sh
#!/bin/bash
While True
Do
load= w|head-1|awk-f ' Load average: ' {print $} ' |cut-d. -f1
If [$load-GT 10]
Then
/usr/local/sbin/mail.py [email protected] "load High" "$load"
Fi
Sleep 30
Done

Case 2

[Email protected] shell]# vim while2.sh
#!/bin/bash
While:
Do
Read-p "Please input a number:" N
If [-Z "$n"]
Then
echo "You need input sth."
Continue
Fi
n1=echo $n|sed ‘s/[0-9]//g‘
If [-N "$n 1"]
Then
echo "You just only input numbers."
Continue
Fi
Break
Done
Echo $n

[[email protected] shell]# sh while2.sh Please input a number: sssyou just only input numbers.Please input a number: ....you just only input numbers.Please input a number: 00

20.13 break jump out of the loop

#!/bin/bash
For I inseq 1 5
Do
? ? Echo $i
? ? if [$i = = 3]
? ? Then
? ?? ??? Break
? ? Fi
? ? Echo $i
Done
Echo AAAAAAA

Operation Process

[Email protected] shell]# vim break.sh

#!/bin/bash
For I inseq 1 5
Do
Echo $i
If [$i-eq 3]
Then
Break
Fi
Echo $i
Done
Echo aaaaaa

[[email protected] shell]# sh break.sh 11223aaaaaa

20.14 continue ends this cycle

Ignore the code under continue and proceed to the next loop directly
#!/bin/bash
For I inseq 1 5
Do
? ? Echo $i
? ? if [$i = = 3]
? ? Then
? ?? ??? Continue
? ? Fi
? ? Echo $i
Done
Echo $i

Operation Process

[Email protected] shell]# vim continue.sh

#!/bin/bash
For I inseq 1 5
Do
Echo $i
If [$i-eq 3]
Then
Continue
Fi
Echo $i
Done
Echo aaaaaa

[[email protected] shell]# sh continue.sh 112234455aaaaaa

20.15 exit exits the entire script

[Email protected] shell]# vim exit.sh

#!/bin/bash
For I inseq 1 5
Do
Echo $i
If [$i-eq 3]
Then
Exit
Fi
Echo $i
Done
Echo aaaaaa

[[email protected] shell]# sh exit.sh 11223

2018-05-31 Linux Learning

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.