Shell BASICS (9) functions, shell Basics

Source: Internet
Author: User

Shell BASICS (9) functions, shell Basics

Functions allow us to divide a complex function into several modules, so that the program structure is clearer and the code reuse rate is higher. Like other programming languages, Shell also supports functions. Shell functions must be defined before use
1. The Definition Format of Shell functions is as follows:

[function] fun_name () {command1[retrun n]} 

The return Value of the function can be explicitly added. If no return statement is added, the result of the last command is used as the return value.

The return value of a Shell function can only be an integer. It is generally used to indicate whether the function is successfully executed. 0 indicates that the function is successful. Other values indicate that the function fails.
If you return other data, such as a string, you will often get an error message: "numeric argument required ".
To call a function, you only need to provide the function name without brackets.
The test example is as follows:

#! /Bin/sh # author: lottu # Copyright (c) li0924.comfunction lottu () {echo "HI lottu" read-p "please input your name: "nameecho" HI $ {name} "} lottu # Call the function echo $? # Print the execution result of the function as follows:./fun01.shHI lottuplease input your name: li0924HI li09240

2. delete a function
Like a variable, the unset command can be used to delete a function, but the. f option must be added as follows:

unset .f function_name

If you want to call a function directly from the terminal, you can define the function in the. profile file in the main directory, so that after each login, you can immediately call the function by entering the name after the command prompt.

3. Function Parameters
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 and $2 indicates the second parameter.
Note: $10 cannot obtain the tenth parameter. To obtain the tenth parameter, $ {10} is required }. When n> = 10, you need to use $ {n} to obtain the parameter. Dressing theory.


Use of shell functions

Functions in shell are different from those in c. C. to return a value, either use a global variable, return, or pointer.

In shell, you need to get the value after shell function processing. Either write the processing result to the file or output it.
I will give an example here, but this is not convenient.
[Root @ localhost Desktop] # cat test. sh
#! /Bin/bash

Print ()
{
Echo "hello world"
Echo "world hello"
}

A = 'print'
Echo $ {}
[Root @ localhost Desktop] # sh test. sh
Hello world hello
[Root @ localhost Desktop] #

When calling a function in shell, there is output. A function call has a return value, which is generally specified by return. If this parameter is not specified, the return value of the last statement in the function is used by default.

How are shell functions defined?

The function body cannot be empty.
Write a statement, such as echo "test"
No error will be reported.
In addition, the general function keyword can be omitted.
The three methods are as follows:
Function fname ()
{
Program Section
}
Function fname
{
Program Section
}
Fname ()
{
Program Section
}

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.