Shell gets the return value of the function

Source: Internet
Author: User

Background: defines a function that is different than the number of local and online server clusters, and wants to get the number of distinct clusters. The function return value of the shell and other languages is quite different.   Define a functionfunctionname () {Action Contentecho Output Contentreturn value #返回值可有可不有}  get the return value of a function  1. The function defaults to passing the standard output, not the return value. so if you call functionname directly, you're actually passing the output backFor example:a= ' functionname 'pass the standard output of the function functionname to a  2. Call function does not need to add (), directly write the function name can befunctionname  3. Use $? To receive the return value status of the previous program, which is the value of returnfunctionnameecho $?The output here is the return value of the function.  4. The return value of the function is generally 0 and 1, indicating success and failure, if it is important to return other integers, define a global variable, change it in the function problems encountered:assign a function return value times wrong: return:diffrentcluster:numeric argument required Although not affected by the use, but it is always bad to look at the shell's function is returned by default is 0 and one indicates whether it is successful, if you must return a value, you can define a global variable outside the function, and then assign the variable in the function. When the function is called, the global variable is changed to achieve the same effect . example: Adding num values through a function#!/bin/shnum=10Add () {echo "Test"((num++))} Add The result of the output is 11 (variable self-increment I use num=num+1 is invalid, do not know why, must use num++)  otherthe parameter definition for a function in the shell is not the same as in other languages, and is not enclosed in parentheses. Functions are defined directly in the function body using the $, $ ... $n to represent parameterscalled directly functionname para1 para2. Paran to invoke the function 

Shell gets the return value of the function

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.