Shell Script Learning Note nine: Shell functions

Source: Internet
Author: User

The definition format of the function:

function Funname ()

{

Action

return xxx;

}

1, can be with function fun () definition, can also be directly fun () definition, without any parameters

2, the parameter returns, can display add: return returns, if not added, will run the result as the last command, as the return value. return followed by value n

1. function without return statement

Demofun () {    echo" This is a shell function "echo" -----Function starts executing----- " Demofun Echo " -----function is finished----- "

2. function with return statement

 funcwithresult () { echo  "   This function adds two numbers to the input   ;  echo   "     Enter the first number:   " ;    Read Anum;  echo   "     Enter a second number:     echo   "  The sum of the two numbers entered is: $?   " 

Note:

1, function return value after calling this function through $? To obtain;

2, all functions must be defined before use, the calling function only uses the name of its functions;

3. Function parameters

Demofun () {Echo "The first parameter , $"    Echo "The second parameter, a"    Echo "Tenth parameter $ A"    Echo "Tenth parameter ${10}"    Echo "The total number of parameters is $#"    Echo "output all parameters as a string $*"} demofun1 2 3 4 5 6 7 8 9  the  -

Note: $ $ cannot get the tenth parameter, and the tenth parameter requires ${10}. When n>=10, you need to use ${n} to get the parameters

Special parameters:

$# the number of arguments passed to the script

$* Displays all parameters passed to the script in a single string

$$ The current process ID number for the script to run

$! 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.

$- shows the current options that the shell uses, as is the SET command function.

$? Displays the exit status of the last command. 0 means there is no error, and any other value indicates an error.

Shell Script Learning Note nine: 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.