Shell--9, Shell functions

Source: Internet
Author: User

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; [return 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 Finishes-----"

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/bashfunwithreturn () {echo ' This function adds the two numbers you enter ... "echo" enters the first number: "Read Anum echo" Enter the second number: "Read    Anothernum echo "Two numbers for $aNum and $anotherNum, respectively!" Return $ (($aNum + $anotherNum))}funwithreturnecho "The sum of the two numbers entered is $?!"

The output resembles the following:

This function adds two numbers to the input ... Enter the first number: 1 Enter the second number: 22 numbers are 1 and 2, respectively! The sum of the two numbers entered is 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.

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 $!"    echo "The tenth parameter is $ A!"    echo "Tenth parameter is ${10}!"    echo "11th parameter is ${11}!"    echo "The total number of parameters is $#!" echo "Output all parameters as a string $*!"} Funwithparam 1 2 3 4 5 6 7 8 9 34 73

Output Result:

The first parameter is 1! The second parameter is 2! The tenth parameter is 10! The tenth parameter is 34! The 11th parameter is 73! The total number of parameters is 11! output all parameters as a string 1 2 3 4 5 6 7 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.


This article is from the "Wind Trace _ Snow Tiger" blog, please be sure to keep this source http://snowtiger.blog.51cto.com/12931578/1942033

Shell--9, Shell 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.