function of the shell

Source: Internet
Author: User
Tags aliases function definition
a function

There is no obvious difference between a function and a shell script, but there is an essential difference in execution:

for a separate shell script, create a new shell process for it when executing, and interpret and execute the code in the script

when the script executes, the shell process automatically finishes

for a function, the call does not create a shell process alone, but instead directly interprets and executes the code in the function in the caller's shell process

function Definition:

Function_name ()

{

Statement1

}

(You can also add the function keyword, which must be defined before it is called)

Example:

#! /bin/bash

#定义函数

Functionsayhello ()

{

echo "Hello, world!"

}

#调用函数

SayHello

function Call:

FUNCTION_NAMEPARAM1 param2

function Links:

The process of invoking another function in a shell function

Example:

#! /bin/bash

#定义函数john ()

John ()

{

echo "Hello, this is John."

}

#定义函数alice

Alice ()

{

#调用函数john

John

echo "Hello, this is Alice."

}

#调用函数alice

Alice

Result: Hello, this is John.

Hello,this is Alice.

function return value:

The shell can return a value using the return statement, returning only 0-255 direct integer values, and the return statement in the function is actually the exit status code used to return the function.

Example:

#! /bin/bash

#定义求和函数

SUM ()

{

Let "z = $ + $"

#将和作为退出状态码返回

Return "$z"

}

#调用求和函数

Sum 22 4

#输出和

echo "$?"

Results: 26

(more than 255 will go wrong, such as Sum 253 4)

To set an alias:

Alias name= "Command"

(name is the name to be specified and command is the original shell command)

function and alias differences:

users cannot specify aliases for a set of commands

parameter list cannot be manipulated through system variables in aliases

when you do not use an alias, Unalisa name
two function Parameters 2.2 Getting the number of function arguments

through $ #来获取脚本参数的个数

#! /bin/bash

#定义函数

Func ()

{

#输出参数个数

echo "The function has $ #parameters."

}

#调用函数

Func a b c d E F Ghello

Func 3 "Hello World"

Func

(if there are spaces in the argument, enclose them in quotation marks) 2.2 Receive parameter values by positional variables:

$: script name

$: First parameter

$ A: the second parameter

$@ and $*: all parameters

Cases:

#! /bin/bash

#定义函数

Func ()

{

#输出所有的参数hello World

echo "All parameters is $*"

#输出所有的参数hello World

echo "All parameters is $@"

#输出脚本名称

echo "The script ' s name is $"

#输出第1个参数 Hello

echo "The first parameter is $"

#输出第2个参数 World

echo "The second paramter is $"

}

#调用函数

Func Hello World 2.3 moving positional parameters

shift command to move all parameters of the script one position to the left

#! /bin/bash

#定义函数

Func ()

{

#通过while循环和shift命令依次获取参数值, $ #获取传递给函数的参数个数

while (($# > 0))

Do

#shift命令参数位置左移一个, the original second parameter will be the first one, each output will be

echo "$"

Shift

Done

} 2.4 receive function parameters via Getopts

getopts Gets the options for the function and the parameter values, or the command-line options for the script and the parameter values

Getopts Optstring[args]

The parameter optstring contains a list of option names that can be recognized for the getopts command. If a colon is followed by an option name, it means that the user can supply parameter values for the option, and the parameter value is saved to a system variable named $optarg.

getopts iterates through each option, and the option name is saved to the args variable.

#! /bin/bash

#定义函数

Func ()

{

#逐个接收选项及其参数

While getopts "A:b:c" arg

Do

When #当指定了-a option

Case ' $arg ' in

A

Parameter values #输出-a option

echo "A ' s argument is$optarg"

;;

b

echo "B ' s argument is$optarg."

;;

C

echo "C"

;;

?)

#未知选项

echo "unkown argument."

Exit 1

;;

Esac

Done

}

#调用函数

Func-a Hello-bworld

Result: A ' s argument is hello

B ' s argument is world

(A, B, followed by a colon, can contain parameters, C does not contain parameters, during the loop, the current option name is saved to ARG, the parameter value of the option is saved to the system variable $optarg.) When the user provides the-C option Yes, just the simple output character C) 2.5 indirect parameter passing:

The transfer of parameters of a function is implemented by indirect variable references. If the value of a variable is another variable's name, the variable is called an indirect variable.

Var=name

Name=john

The second variable can be referenced in two ways:

${name}

${!var}

(Indirect reference syntax in Shell is ${!var_name}) three function library files

the difference between a script and a library file is that the function library file includes only functions, and the script can include definitions of functions and variables, and also code that executes.

The library file has the main program loaded and executed, the user does not have to have the library file execution permissions, as long as the Read permission. 3.1 Call to function library file

. FileName (There is a space between the dot and the file name)

#! /bin/bash

#载入函数库

. lib.sh

#定义变量

Msg= "The Fileis not found."

#调用函数库中的函数

Error $msg

lib.sh file:

#! /bin/bash

#定义函数

Error ()

{

#1和2都是文件描述符, 1 for flag output, 2 standard error output,>& copy an output descriptor

echo "ERROR:" $@ 1>&2

}

Warning ()

{

echo "WARNING:" $@ 1>&2

}

Result: Error:the file is not found 3.2 recursive function

The calling process is calling itself repeatedly.

#! /bin/bash

#定义递归函数

Fact ()

{

#定义局部变量

Local n= "$"

#当n等于0时终止递归调用

If ["$n"-eq 0]

Then

Result=1

Else

#当n大于0时, the factorial of n-1 is calculated recursively

Let "m=n-1"

Fact "$m"

Let "result= $n * $?"

Fi

#将计算结果以退出状态码的形式返回

return $result

}

#调用递归函数

Fact "$"

echo "Factorial of $ is $?"

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.