Shell Foundation Four: Functions, Dead Loops, and for,while special usages

Source: Internet
Author: User

One: the Dead cycle

Syntax 1:

While True;do

Loop body

If Condition;then

Break

Fi

Done

Syntax 2:

until False;do

Loop body

If Condition;then

Break

Fi

Done

is to keep looping until a certain condition comes to an end .


Cases:

See if a user with "Gentoo" on the current system is logged in every 3 seconds

If you are logged in, the Gentoo is already logged in, and if you are not logged in, the display is still not logged in and shows how many times it was viewed

[[Email protected]_110 shell]# Cat 12.sh#!/bin/bash # Replace "Gentoo" with a variable and the corresponding command to enable the service or user to go offline alert #declare-I i=0while t      Rue;do if Who|grep "Gentoo" &>/dev/null;then echo "The Gentoo is logged" break fi let i++ echo "$i Gentoo is not login" sleep 3done


Second, while, for special use

Special usage of the while loop:

Traverse each line of a file

Grammar:

While read Variable;do

loop Body

Done </filename

Example: Find all users with even-numbered UID, display their user name and UID

[Email protected]_110 shell]# cat 13.sh#!/bin/bash#while read Line;do userid=$ (echo $line |cut-d:-f3) If [$[$ USERID%2]-eq 0];then echo $line | cut-d:-f1,3 fidone</etc/passwd[[email protected]_110 shell]# bash 13.shroot:0daemon:2mail:8haldaemon:68ntp:38ssh d:74tcpdump:72gentoo:500



Special usage for the For loop

Grammar:

For ( (EXPR1;EXPR2;EXPR3));d o

Loop body

Done

expr1: defines the control variable and assigns the initial value

expr2: cyclic control conditions

EXPR3: correcting control variables


Example: To find the sum of all positive integers within 100

[Email protected]_110 shell]# Cat 14.sh#!/bin/bashfor ((i=1;i<=100;i++));d o let sum+= $idoneecho "The sum is $sum" [[ Email protected]_110 shell]# bash 14.shThe sum is 5050

Print 99 multiplication table

[[email protected]_110 shell]# cat 15.sh#!/bin/bash#for  ((x=1;x<=9;x++));d o              #  in this particular format, the number comparison can also be used directly with the <= number.     for  (y=1;y<= $x; y++);d o       echo -n  -e  "${y}x${x}=$[$y * $x]\t"     done    echodone[[email  Protected]_110 shell]# bash 15.sh1x1=11x2=2   2x2=41x3=3   2x3=6    3x3=91x4=4   2x4=8   3x4=12  4x4=161x5=5    2x5=10  3x5=15  4x5=20  5x5=251x6=6   2x6=12  3x6 =18  4x6=24  5x6=30  6x6=361x7=7   2x7=14  3x7=21   4x7=28  5x7=35  6x7=42  7x7=491x8=8   2x8=16  3x8 =24  4x8=32  5x8=40  6x8=48  7x8=56  8x8=641x9=9   2x9=18   3x9=27  4x9=36  5x9=45  6x9=54  7x9=63  8x9=72   9x9=81[[email protected]_110 shell]#



Third, function

Function: Functions

Encapsulate a piece of code with independent functionality and give it a name, followed by a call to the whole code directly from the given function name

role of the function :

Code Reuse

Modular programming

How to use the function:

Define first: Writing function Code

Call after: give the function name and pass the parameters on Demand

Define the method:

Syntax 1:

function NAME {

function body

Syntax 2:

NAME () {

function body

Custom function state return value:

return [#]

0: Success

1-255: Failure

Note: When the function code executes, once the return is encountered, the function code terminates and the function returns;


















Modular programming

Function: Separate the code in the script file into multiple segments and put it in a different file

Suppose the/root/bin/srv directory has two files:

(1) Function file

(2) Script file


Using configuration files for scripts

Only variables are defined in a file

Script file Source This variable defines the file


Scope of the variable:

Local variables:

Local Variable=value


Survival time:

function execution begins, until function returns to the end;



Shell Foundation Four: Functions, Dead Loops, and for,while special usages

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.