Functions in the shell

Source: Internet
Author: User
Tags terminates variable scope

What is a function?

1) Basic concept of function

functions:function, refers to the code of a separate function as a whole and take a name for it, that is, named code snippet, also known as function;

function is an important component of implementing code reuse in procedural programming, which is the important component of modularization programming and structured programming .

The function code snippet is not automatically executed when it is defined, it is executed automatically when called, and the function code snippet represented by the function name is executed when the code executes, given the function name in the code;

The function naming is similar to the variable naming law, try not to have the same name as the system command;

the life cycle of a function is created when it is called and terminates when it is returned (that is, when the function is finished);

the function state return value is the state return value of the last command run in the body of the function, which is not ideal for a state return value like a script, so you sometimes need to customize the function state return value; Custom Function state return value use return command, the return function terminates at any position in the function body:

return [0-255]

0: Success

1-255: Failure

(2) Definition of function

format one:

function F_name {

... function Body ...

Format two:

F_name () {

... function Body ...

}

(3) function return value

① function Execution Result return value

1> output using the echo or printf command;

printf does not wrap output but can format the output;

2> the execution result of the command called in the body of the function;

The execution state return value of the ② function (that is, exit status code)

1> default depends on the exit status code of the last command executed in the function body;

2> Custom: Use the return command;


(4) function parameters

in the function body you can use $1,$2, ... Reference the arguments passed to the function, and you can use $* or [email protected] in the function to refer to all arguments passed to the function, using $ #引用传递给函数的参数个数;

when calling a function, the given argument list is separated by a whitespace character after the function name, for example,testfunc arg1 arg2 arg3 ...

(5) Variable scope:

Local Variables: scope is the life cycle of a function;

methods for defining local variables:local Variable=value

local variable: scope is the life cycle of the current shell script;

(6) function recursion: function calls itself directly or indirectly;

① factorial: n!=n* (n-1)!=n* (n-1) * (n-2)! = ...

#/bin/bash#fact () {if [$1-eq 1-o $1-eq 0];then Echo 1else echo "$[$1*$ (Fact $[$1-1])]" Fi}fact $

Fibonacci sequence: This series from the 2 entry, each item equals the sum of the first two items Span style= "font-family: ' Times New Roman '; font-size:14px;" >, special note 0 item is 0 1 item is 1

#!/bin/bash#fab ()  {        if [ $1 -eq 0  -o $1 -eq 1 ];then                 echo -n  "1 "         else                 echo -n   "$[$ (Fab $[$1-2]) +$ (fab $[$1-1])] "         fi}for  i in $ (seq $1);d o                                                                                                                                                 fab  $idoneecho ~






This article is from the Linux OPS blog, so be sure to keep this source http://arm2012.blog.51cto.com/2418467/1961195

Functions in the shell

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.