009-shell function

Source: Internet
Author: User
Tags function definition

First, function definition

The Linux shell can be user-defined functions and can be called casually in shell scripts.

The functions in the shell are defined in the following format:

function ] Funname [()]{    action;     int ;]}

Description

    • 1, can be with function fun () definition, you can also directly fun () definition, without any parameters.
    • 2, the parameter returns, can display add: return returns, if not added, will run the result as the last command, as the return value. Return followed by a value of N (0-255

The following example defines a function and makes a call:

#!/bin/Bashdemofun () {    echo" This is my first shell function! " }echo"-----function starts execution-----"demofunecho"  -----function is finished-----"

Output Result:

-----function starts executing----- This is my first Shell function !-----The function is complete-----

The following defines a function with a return statement:

#!/bin/bash# Author: Rookie Tutorial # Url:www.runoob.comfunWithReturn () {Echo "This function adds two numbers to the input ..."    Echo "Enter the first number:"Read AnumEcho "Enter a second number:"Read AnothernumEcho "two numbers are $aNum and $anotherNum, respectively!"return $ ($aNum+$anotherNum))} FunwithreturnEcho "the sum of the two numbers entered is $?!"

The output resembles the following:

1212 !  3 !

function return value after calling this function through $? To obtain.

Note: All functions must be defined before they are used. This means that the function must be placed at the beginning of the script until the shell interpreter discovers it for the first time before it can be used. The calling function uses only its name of functions.

Second, function parameters

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 ...

Examples of functions with parameters:

#!/bin/Bashfunwithparam () {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 $*!"}funwithparam1 2 3 4 5 6 7 8 9  the  the

Output Result:

 The first parameter is 1 !  The second argument is  2 !  The tenth argument is  10 !  The tenth argument is  34 !  The 11th argument is  73 !  11 !  output all parameters as a string  1  3  4  5  6  8  9  34  73 ! 

Note that the $ $ cannot get the tenth parameter, and the tenth parameter requires ${10}. When n>=10, you need to use ${n} to get the parameters.

In addition, there are several special characters for handling parameters:

parameter Handling Description
$# The number of arguments passed to the script
$* Displays all parameters passed to the script in a single string
$$ The current process ID number for the script to run
$! ID number of the last process running in the background
[Email protected] Same as $*, but quoted when used, and returns each parameter in quotation marks.
$- Displays the current options used by the shell, the same as the SET command function.
$? Displays the exit status of the last command. 0 means there is no error, and any other value indicates an error.

009-shell function

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.