[9-13] Shell series 7--function and parameter transfer

Source: Internet
Author: User
Tags function definition

The purpose of this paper is to review the function, definition, nesting, deletion, terminal call, return value, parameter passing of shell functions.

Knowledge Reserve

  • function: That is Functions , a set of independent code modules that implement a common function (usually inside variables, variable parameters) are encapsulated together and given a name; When used, the whole code can be called directly by the given function name.

  • Function action:<1> code reuse <2> Modular programming

  • letter number is defined using:<1>: Write function code <2> call: Call function, pass parameter on demand [not required]

function definition and invocation

1
2
3
4
5
6
7
8
9

One


function  fun_name () {
     function body
    
    [ return  value ]
}
#--------------------------->#
Span style= "color: #000000;" >
Fun_name () {
     function body
    
    [  return &NBSP;VALUE&NBSP;
}

<1> First, use the keyword function to declare the name of the function

<2> Two, directly with the function name, not much difference in nature, for readability, it is recommended to use the first

Span style= "line-height:0px;" > calling method : Use function name directly, no need to take

1
2
3
4


sum ()  {
    echo  "$[$1+$2]"
}
sum  $1 $2


function nesting

Functions can be nested between each other and can be used when a function relies on another small function, for example

Implementation code

1
2
3
4
5
6
7
8
9

One


#!/bin/bash

Function fun1   ()  {
    echo  "this is  Fun1. "
}

function fun2  ()  {
    echo   "this is fun2."     fun1 
}
fun2

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/73/94/wKiom1YBO_KT78LgAANEKHa91ds154.bmp "title=" Image 1.bmp "alt=" Wkiom1ybo_kt78lgaanekha91ds154.bmp "/>


function Delete

variables can be deleted using unset, then the function can also be deleted, but who would have such an egg ache?

1


Unset . F function_name


Terminal call

If you want to use your own defined function at the terminal, you can put the function definition in a file such as. Profile

of course the profile file, based on what you learned before

Global Profile --->/etc/profile/etc/profile.d/*.* [many script files are self-executing after booting]

Profile ---> ~/.bash_profile

Example 1 : Defines a function sumsum for all users to find a 2-digit fit, and can be called directly at the terminal

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/73/91/wKioL1YBPjLjfS_MAAV5-CPHOrg137.bmp "title=" Image 2.bmp "alt=" Wkiol1ybpjljfs_maav5-cphorg137.bmp "/>

After you have defined the function in/etc/profile, you need to reload the profile [logoff or Source/etc/profile].

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/73/91/wKioL1YBPqOS_R9oAAPrWP6OS1c916.bmp "title=" Image 3.bmp "alt=" Wkiol1ybpqos_r9oaaprwp6os1c916.bmp "/>


return value

There are 2 results of command execution: execution result + Execution status result. The execution status result is a value of 0-255, 0 succeeds, and the other fails.

The function return value is actually the state result, which is used to indicate whether the function succeeds or not, that is, the value of this special variable.

users can use [return value] Custom Function Values

: function execution to define a custom return value

1
2
3
4
5
6
7


#!/bin/bash

Function fun1   ()  {
    echo  "this is  Fun1. "
     return Span style= "color: #000000;" >  188
}
fun1

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/73/91/wKioL1YBQEPSakVUAAP6KJdDdnE724.bmp "title=" Image 4.bmp "alt=" Wkiol1ybqepsakvuaap6kjdddne724.bmp "/>

Note the point:

The <1> function return value can only be an integer of 0-255, where 0 identifies success and 1-255 indicates failure

When the <2> function code executes, once the return is encountered, the function code terminates execution, returning the custom function value [if any]

The benefit of the <3> function return value is that if a program causes errors in a variety of situations, it is much easier to maintain and debug if each error defines the function return value separately.


Parameter passing

Parameter passing is not just for the function body to call, and any script code that you want to invoke requires only a syntax format to write out the calling statement.

< /span>

: Pass multiple parameters for the script, and call these parameters

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/73/95/wKiom1YBTAKyW5s7AAEICjQixI4557.jpg "title=" Image 1.png "alt=" Wkiom1ybtakyw5s7aaeicjqixi4557.jpg "/>

Implementation code

1
2
3
4
5
6


#!/bin/bash

echo  "the first paramater is  $1"
echo  "the second paramater is $2"
echo  " paramaters counts   -->  $# "
echo  "all paramaters are -->  [email protected]"

The Red box "1 2 3" is the pass parameter to the script, where the $, $ is a reference to the parameters passed, in fact, the essence is a special variable.

$# The total number of parameters passed, [email protected] printout full parameter. There are other special variables and usages--reference [9-13]shell series 2--variable base

This article is from the "Blue Warehouse" blog, be sure to keep this source http://bluebox.blog.51cto.com/8852456/1697187

[9-13] Shell series 7--function and parameter transfer

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.