Shell Script--functions

Source: Internet
Author: User

The shell's functions are the same as Javacript and PHP's function declarations, except that when the shell calls the function, it can call the function simply by writing the function name, noting that parentheses are not appended to the function name.

Here is a simple function to use

#!/bin/bash# file name: Test.shfunction Test () {    echo "aaaaaaa"} #直接使用函数名就可以调用函数testtest

Run:

  

If the function requires arguments, not the write arguments after the function name when the function is defined, but inside the function, like a command line line that uses $1-9 to get the arguments passed to the function. Then, when the function is called, arguments are given to the function, followed by the arguments directly after the function name, separated by a space between the arguments and function names.

#!/bin/bash# file name: Test.shfunction testone () {    num=$1    tot=0 #求1到num的和 for    ((i=1;i<= $num; i++)) {        tot=$ (($tot + $i))    }    echo $tot}function testtwo () {    # echo ' expr $ + $ '    #等价于    echo $ (($1+$2))}testone 100TESTTWO 200 300

Run:

  

Shell Script--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.