Shell Programming (Functions)

Source: Internet
Author: User

declaring functions

Demofun () {    echo" This is my first shell function! " }

Function name () {

... function body

}

In the shell, arguments can be passed to a function when it is called. Inside the function body, the value of the parameter is obtained in the form of a $n, for example, $ $ for the first argument, and $ = for the second argument ...

Funwithparam () {Echo "The first parameter is $!"    Echo "The second parameter is a $!"    Echo "the tenth parameter is $ A!"    Echo "the tenth parameter is ${10}!"    Echo "the 11th parameter is ${11}!"    Echo "The total number of parameters is $#!"    Echo "output all parameters as a string $*!"}

Function call

Function Name parameter 1 parameter 2

Eg: Enter an IP address the last one to start, automatically find out which host address in the network segment online

#!/bin/Bash#checkonline () {ADDR=$1        if Ping-C1$ADDR &>/dev/NULL         Thenreturn0        Elsereturn1        fi}if[" $"-gt0-A" $"-gt0-A" $"-lt" $"]2>/dev/NULL Then         for((i=$1; i<$2; i++))         Do                ifCheckonline192.168.1. $i Then                        Echo "192.168.1. $i is online"                fi         DoneElse        Echo "parameter is not correct"fi

Summarize

A method of judging whether a variable is a numeric type

# # method 1#if["$"-gt 0] 2>/dev/null; then#  Echo "is number ." #else #  Echo 'No.' #fi# # Method 2, Case # Case " $" inch #  [1-9][0-9]*)  #    Echo "is number ." #    ;; #  *)  #    ;; #Esac# # Method 3,Expr Expr$1 "+" Ten&>/dev/NULLif[$?-eq0]; Then  Echo " $ is number"Else  Echo " not number"fi

Shell Programming (Functions)

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.