Fifth. Definition, execution, parameters and recursive functions of shell functions

Source: Internet
Author: User

Fifth. Definition, execution, parameters and recursive functions of shell functions

Bash (Bourne Again Shell) also supports functions, when writing large complex scripts, you can use functions to write code into a function of a relatively independent code block, the code block, structure clearly, effectively reduce the code of the program. But the bash shell is an explanatory language, and execution efficiency is not as high as a compiled language.

Definition of Shell function
格式一:(function name() {    command sequence (命令序列)}格式二:name() {    command sequence (命令序列)}
function execution
[[email protected] ~]# function name() {> echo "123"> }执行[[email protected] ~]# name    #直接调用函数名即可123
Passing parameters
[[email protected] ~]# vi chuandi.sh#!/bin/bashaa="111"    #定义全局变量bb="222"    #定义全局变量function name() {           #定义函数名         local cc="ccc"      #定义局部变量        local dd="ddd"      #定义局部变量        echo $aa, $bb       #打印全局变量        echo $cc            #打印局部变量        return 0            #shell函数返回值是正行,并且在0-257之间。}echo $dd    #运行局部变量,因为这里是在函数外运行,不会生效。name    #运行函数name注意:$aa是第一个参数$1, $bb是第二个参数$2, 依次类推$n就是第n个参数$nreturn 0 参数返回,可以显示加:return 返回,如果不加,将以最后一条命令运行结果作为返回值
执行:[[email protected] ~]# /bin/bash chuandi.sh 111, 222ccc
Recursive functions

Bash also supports recursive functions (functions that can call itself)

[[email protected] ~]# cat digui.sh #!/bin/bashfunction name() {        echo $1        name hello        sleep 1}name执行脚本会不停的打印hello,按ctrl+c 手动结束
Recursive classics: forkxxx

Probably a lot of people have heard of forkxxx, it is actually a very simple recursive program, the program does only the same thing: the recursive function can call itself, constantly generate new processes, which will lead to this simple program quickly exhausted all the resources inside the system, resulting in a denial of service attack.

.(){.|.&};.
    • Line 1th describes the following to define a function, the function name is a decimal point, there are no optional parameters.
    • Line 2nd indicates the start of the function body.
    • The 3rd line is what the function body really wants to do, first it calls the function recursively, and then invokes a new process with the pipeline (the thing it does is recursively calls the function) and puts it in the background.
    • Line 4th means end of function body
    • Line 5th does not do anything, and is used to separate two commands from the command line. Overall, it shows that the program contains two parts, first defining a function, and then calling the function.
    • Line 6th means calling this function

Fifth. Definition, execution, parameters and recursive functions of 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.