Shell functions use

Source: Internet
Author: User
Tags function definition

function definition Format:

It's a bit like JS, but the function keyword is optional in the shell.

Such as:

sum{    //Shell statement}

  

function sum () {    //Shell statement}

Function Call Method:

Sum parameter 1 parameter 2 ...

  

Get function Parameters:

This may not be the same as our previous experience, and the way in which you get function parameters in the shell is a bit special.

In the general language, a variable name is given in parentheses after the function name to receive the function arguments.

In the shell, the way to get a function parameter is to use a dollar sign in the body of the function plus a numeric gain, such as to get the first argument, $ $ to get the second argument, and so on, but when it is greater than or equal to 10, the number after the $ is appended with braces, such as ${10}.

Such as:

Sum () {    echo $ (($ + $)}

  

Additional parameters related to $:

Parameter handling Description
$# Total number of arguments passed to the function
$* Display all parameters in a single string
$$ ID number of the current process that the script is running
$! ID number of the last process running in the background
[Email protected] Same as $*, but quoted when used, and returns each parameter in quotation marks
$- Displays the current options used by the shell, such as: when we use Set-x, the $-contains X
$? Displays the exit status of the previous command, or the return value, which returns 0 if no value is returned and runs normally.

  

Such as:

#!/bin/bashfunca () {    return 123}sum () {    echo] parameter total: $# "    echo" string to display all parameters: $* "    echo" script run Process ID number: $$ "    echo "Display all parameters in string form: [email protected]"    echo "shell script options: $-"    Funca    echo "The return value of the previous statement: $?"    Echo $ (($ + $))}sum 1 2

The above code will output:

Total number of parameters: 2 Displays all parameters as a string: 1 2 The process ID number that the script runs: 1408 displays all parameters as a string: 1 2shell the option to run the script: The return value of the previous statement of HB: 1233

  

Get any number of arguments in the script:

#!/bin/bashsummulti () {    sum=0 while    [""! = ""]    do        sum=$ (($sum +$1))        shift    done echo $sum;} Summulti 1 2 3 4 5

  

A space is required around the middle bracket in the back of the while, syntax is required, and the writing format is free of general language.

and the assignment expression of the equal sign can not have a space, a statement with a space, the shell will be the first word as a shell command, and then the first space behind any content as a shell command parameters, which is the shell's unique, understanding this point for the shell learning is very critical! As sum=0 above, if you write sum = 0, you will be prompted for something like the following:

Sum: =: No such file or directorysum:0: No such file or directory

That is, when the shell runs this statement, it actually treats sum as a shell command (and of course the function can call it).

The function of shift is to remove the first parameter from the argument list.

Gets the function return value:

1. Get the return value by return:

#!/bin/bashsummulti () {    sum=0 while    [""! = ""]    do        sum=$ (($sum +$1))        shift-Done    return $sum;} Summulti 1 2 3 4 5echo "Get Return value by return: $?"

Can we use the $ mentioned above? Gets the return value of the previous function execution.

2. Get the return value via Echo: (principle: We can use anti-quotes to get the output of the command execution)

#!/bin/bashsummulti () {    sum=0 while    [""! = ""]    do        sum=$ (($sum +$1))        shift-Done    echo $ sum;} Sum= ' Summulti 1 2 3 4 5 ' echo ' gets the return value via Echo: $sum "

The line of the above function call can also be changed: The two are similar in wording

sum=$ (Summulti 1 2 3 4 5)

  

  

Shell functions use

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.