6.shell function
6.1. Defining functions
Brief introduction:
The shell allows a set of command sets or statements to form an available block that becomes a shell function
Defining the format of a function
Method One
Function name ()
{
Command 1
......
}
Method Two
Function name ()
{
Command 1
......
}
Two ways to define a function
Functions can be placed in the same file as a piece of code, or in a separate file containing only functions
example, define only the following functions in a single file:
#!/bin/bash#hellofunfunction Hello () { "Hello, today is ' date'" # ' Date ' date is enclosed in anti-quotation marks, indicating that the date command is executed first in this statement and that the result of the date command is replaced with the string of the date position return1}
6.2. Function calls
Example
#!/bin/bash#funcfunction Hello () { "hello,today is ' date '" " now going to the fuction Hello""back from thefunction Hello "
6.3. Parameter passing
Brief introduction
Passing parameters to a function is like using positional variables in a script. $, $ 、... $9
Example
#!/bin/bash#funcfunction Hello () { "Hello, $ today is ' date '" " now going to the fuction Hello""back from thefunction Hello "
6.4. function files
Brief introduction:
You can put function definitions and function calls in the same file, or you can specifically define functions in a file to invoke the function in other files
Example:
Defining functions in File Hellofun
#!/bin/bashfunction Hello () { "Hello, today is ' date '" return 1 }
Call this function in another file func
#!/bin/"now going to thefuction Hello""back From the function Hello"
Suggestions:
When learning shell scripts, you can look at the startup files of the Linux system to learn, but notice in the process, if you want to modify it, be sure to back up this file first, modify it so that you can use the backup file in error to save
6.5. Load and delete functions
Check load functions and delete functions
View load functions
The set command looks at how this function loads the
Example:
#!/bin/bash. Hellofunset"now going to thefuction Hello" "back from thefunction Hello"
Delete a function
unset command
Example
#!/bin/bash. Hellofunset"now going to thefuction Hello" hello# here will output the message:. /"back from thefunction Hello"
6.6. function return Status value
Example 1:
#!/bin/bashfunction Hello () { "Hello, today is ' date '" return 0 now going to thefunction Hellohello# next to output the return status value, with $? represents echo $? "back from thefunction Hello"
Example 2:
#!/bin/bashfunction Hello () {echo hello, today is ' date ' " return 0 }echo " now going to the function Hello " returnvalue =hello# next to output the return status value, with $ ? Span style= "color: #000000;" > indicates echo $ ? echo $returnvalueecho " back from the function Hello " #注意: Unlike languages such as C, the return value of a function cannot be assigned to a variable #returnvalue =hello in a shell script. Instead of assigning the return value of the Hello function to returnvalue, the Hello string is assigned to ReturnValue
Shell Programming Basics Tutorial 6--shell function