Shell Programming function day3

1. The function returns:

The role of the program is to function in multiple calls to the same code is defined as a part, then a name for this piece of code, all other repeated calls to this part of the code will only use this function name. When the need to modify this part of the code is repeated, only need to change the code of a function can be realized within so there call modification.

The advantage of using the function:

  ① the same definition of a function block, can reduce the code amount of the entire program

  ② increase readability

  ③ functional modular program can be realized, different functions using modular program

  ④ can let the program structure more clearly

  ⑤ same code module repeated calls

He stressed: For the shell, the 2000 linux command system can be said to be a function of the shell.

 

2.shell function syntax:

Syntax:

Simple syntax:

Function name(){

  instruction...

  return n

}

Specification syntax:

function name of the function () {

  instruction...

  return n

}

Tip: shell exit return value is output return value, the function returns the value in return output.

Functions of the Executive 3.shell

call function:

   ① can directly execute the function name (without brackets)

   Function name

   note:

      a. When the function is executed after the function name with parentheses do not

      B. Functions defined functions must be defined in the body of the function name to execute, execute shell row from top to bottom of execution.

  ② with function execution method parameters:

  Function name Parameter 1 Parameter 2

  Tip: mass participation mass participation and script functions are similar, except that the name of the script function names can be replaced

  After the function [] contact Parameter Description

   a.shell positional parameters ($ 1, $ 2, $ 3, $ 4, $ 5, $ #, $ * $? and $ @) can be a function of the parameters.

   Note: Linux shell script some parameters of meaning :

     $ # Is the number of parameters passed to the script

     $ 0 is the name of the script itself
     $ 1 is the first argument passed to the shell script
     $ 2 is the second parameter passed to the shell script
     $ @ Is a list of all the parameters passed to the script
     $ * Is a single string that is displayed all the different parameters passed to the script, and location variables, parameters can be more than 9
     $$ script is running the current process ID number
     $? Exit status of the last command is displayed, and 0 indicates no error, the other indicates an error

   b. At this parent script parameters temporarily overshadowed or hidden function parameters

   c. $ 0 is special, it is still the name of the parent script

   d. When the function is completed, the original command-line script parameter is restored

   e. In the function inside shell, the shell and the return command to exit the similar effect is out of function

   f. Use the exit at the shell will exit the entire function body shell scripts instead of exiting the shell functions

   g.return statement returns an exit value (return value) to the program calls the function

   h. function parameter variables are defined in the function body, if an ordinary variable definitions typically use local i

4. Function Example:

Define a function script:

#!/bin/sh
fun01test(){
    echo "I am a function!!"
}

fun01test ==> call the function fun01test

Reference to another script function in the above function in the script:

[the root @ localhost ~] # fun01.sh CAT
#! / bin / SH
. /root/fun02test.sh ==> to introduce another function resides script file
fun01test () {
    echo "the I AM A function !!"
}
fun01test
fun02 ==> name to perform the required function call

Definition of a function and parameter passing execute:

[the root @ localhost ~] fun02test.sh CAT #
! # / bin / SH
fun02 () {
    echo "This IS First Parameters: $. 1"
}
fun02 111
fun02 AAA
fun02 b1a1
[the root @ localhost ~] # SH fun02test.sh
This IS the parameters First: 111
This iS First the parameters: aaa
This iS First the parameters: b1a1
Note: when you pass parameters to a function, not behind the implementation of the script file with parameters passed, is passed in the function call

Example 1:

  Out function parameter passing command-line script parameter passing, for any given URL to determine whether abnormal.

Step by step answer:

1. Check the web url script parameter passing is normal.

2. Check function written as a function.

3. function parameter passing turn into script command line parameter passing, for any abnormality is determined whether the specified URL.

method one:

#!/bin/sh
[ -f /etc/init.d/functions ]&& . /etc/init.d/functions
usage(){
    echo "USAGE:$0 url"
    exit 1
}
RETVAL=0
CheckUrl(){
    wget -T 10 --spider -t 2 $1 &>/dev/null
    RETVAL=$?
    if [ $RETVAL -eq 0 ];then
       action "$1 url" /bin/true
        
    else
       action "$1 url" /bin/false
    fi
    return $RETVAL
}
main(){
    if [ $# -ne 1 ];then
        usage
    fi
    CheckUrl $1
    RETVAL=$?
    return $RETVAL
}
main $*

Guess you like

Origin www.cnblogs.com/Simplelearning/p/12384467.html