Linux Shell series (15th) Shell function introduction, linuxshell

Source: Internet
Author: User

Linux Shell series (15th) Shell function introduction, linuxshell

This article is part 15 of the Linux Shell series. For more Linux Shell series tutorials, see: Linux Shell series tutorials.

A function can divide a complex function into several modules, so that the program structure is clearer and the code reuse rate is higher.

Functions are supported in advanced languages, and Shell is no exception. Today we will introduce the usage of functions in Shell.

I. Shell function syntax

Because the function is a script language and runs row by row during execution, the Shell function must be defined before use.

The Definition Format of Shell functions is as follows:

[ function ] funname [()]{    command;    [return int;]}

Note: function keywords are optional and can be added or not added.

The final return value is the internal function body of the braces. You can add the [return] keyword to specify the returned content of the function. If this parameter is not added, the last command is run as the return value. Return followed by the value n (0-255 ).

Ii. Example of using Shell Functions

Next we will use examples to learn how to use Shell functions.

Example 1:

#! /Bin/bash # author: Linux Emy # url: www.linuxdaxue.com # date: 2016-06-01demo () {echo "this is my first shell function! "} Echo" ----- function execution started ----- "demoecho" ----- function execution completed -----"

Note: demo is a function definition. The function body has only one sentence: echo "this is my first shell function! "

Then, call the function by the function name.

Program output:

----- Start to execute the function ----- This is my first shell function! ----- Function execution completed -----

This example does not reflect the use of input parameters of the function. Next we will introduce the parameter passing method of the function in Shell.

Iii. Shell function parameter Processing

In Shell, parameters can be passed to a function.

Inside the function body, get the parameter value in the form of $ n. For example, $1 indicates the first parameter, $2 indicates the second parameter, and $0 indicates the script itself.

Example of Shell function parameter Processing

Example 2:

Let's look at another example:

#! /Bin/sh # Author: Linux University # url: http://www.linuxdaxue.com # date: 2016-06-01function fSum () {echo "input parameter:" $1, $2 return $($1 + $2)} fSum 5 7 total = $ (fSum 3 2) echo "Return:" $ total, $?

This defines a calculation parameter and function. The function can get the input parameter value in the form of $1, $2, and then use $($1 + $2 )) in this form, the two values are added and the result is returned.

You can also use the brackets when calling the command. Shell will call the commands in the brackets. Therefore, we can think of a function in shell as a new command, which is a command. Therefore, each input parameter is directly separated by a space.

Output:

Input parameter: 5, 7 Return: input parameter: 3, 2, 5

Note:

Note: $10 cannot obtain the tenth parameter. $ {10} is required to obtain the tenth parameter }. When n> = 10, you need to use $ {n} to obtain the parameter.

Special characters used in Functions

In addition, there are several special characters used to process parameters:

Parameters Description
$ # Number of parameters passed to the script
$ * Display All parameters passed to the script with a single string
$ ID of the current process that the script runs
$! ID of the last process running in the background
$ @ It is the same as $ *, but it is enclosed in quotation marks and each parameter is returned in quotation marks.
$- Displays the current options used by Shell, which have the same function as the set command.
$? Displays the exit status of the last command. 0 indicates no error, and any other value indicates an error.

Now, let's take a look at the knowledge of Shell functions. The functions in Shell are very powerful. I hope you can practice them more and be familiar with them.

For more Linux Shell tutorials, see the Linux Shell series tutorials.

 

  • Copyright:This site's original article, published three months ago by the Linux University (Linuxdaxue.com), contains 1489 words.
  • Reprinted Please note:Linux Shell series tutorial (15th) Shell function introduction | Linux University

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.