Function
Functions have the same functionality:
Simply put, the function of the program is to call the same part of the code in a number of parts, and then a name, so the function is called the name can be, in the modification of the code, only need to modify the function body code.
Advantage:
1, the same program segment is defined as a function, can greatly reduce the code of the program amount
2, increase the readability of the program.
3, the realization of the function of the program module,
4, easy to modify the program
1. Syntax for functions
Simple syntax:
Function name () {
Instruction set ...
return N # #其中return是函数的返回值, as if the return value of the script is exit n
}
================
Syntax of the specification
Function name () {
Instruction set ...
return n
}
2. Function execution
Call Function:
A, directly execute the function name can
Name of function
Attention:
When executing a function, simply execute the function name, without parentheses
The definition of the function and the body of the function must be defined before the name of the function to be executed, and the execution of the shell executes from top to bottom.
b, function execution method to parameter
Function Name parameter 1 parameter 2 ... Parameter n
function with parameter description:
The positional parameters in the function body ($, $, $ $ ... $n, $#, $*, $, and [email protected]) can all be parameters within a function
The parameters of the parent script are temporarily obscured or hidden by the function arguments
$ more special, the name of the parent script is directly
When the function is complete, the principle command-line arguments are restored
Inside the shell function, the return command functions and works in the same way as exit, jumping out of the function
Using exit inside a shell function terminates the entire shell script
The return statement returns a program that exits to the call
Small examples of functions:
========================
[email protected] script]# cat hs.sh
#/bin/sh
Lvnian () {
echo "Hello Lvnian!"
}
Lvnian
Lvnian
Lvnian
Lvnian
[Email protected] script]# sh hs.sh
Hello Lvnian!!
Hello Lvnian!!
Hello Lvnian!!
Hello Lvnian!!
[Email protected] script]# sh hs.sh
Hello Lvnian!!
Hello Lvnian!!
Hello Lvnian!!
Hello Lvnian!!
[Email protected] script]#
========================
[email protected] script]# cat hs.sh
#/bin/sh
Lvnian () {
echo "Hello Lvnian!"
}
Lvnian
Lvnian
Lvnian
Lvnian
echo "======== ' Lvnian ' ============"
Echo ' ======== ' Lvnian ' ============ '
[Email protected] script]# sh hs.sh
Hello Lvnian!!
Hello Lvnian!!
Hello Lvnian!!
Hello Lvnian!!
======== Hello Lvnian!! ============
======== ' Lvnian ' ============
[Email protected] script]#
If the function is called with ECHO. That will be enclosed in double quotes, rather than "let the function execute first, then output
###########################################
The function uses the pass parameter
[email protected] script]# cat hs.sh
#/bin/sh
Lvnian () {
echo "Hello Lvnian $!"
}
Lvnian xiaoming
Lvnian Xiaoqiong
[Email protected] script]# sh hs.sh
Hello Lvnian xiaoming!!
Hello Lvnian xiaoqiong!!
Example:
Using functions and Function arguments, the script implements an exception to the specified arbitrary URL.
[email protected] script]# cat webjk.sh
#!/bin/sh
#status = ' curl-i-s-w '%{http_code}% '-o/dev/null www.baidu.com '
. /etc/init.d/functions
Web () {
Status= ' Curl-i-S $ |awk ' Nr==1{print $ (NF-1)} '
If [-Z $status]
Then
Action "is fail!"/bin/false
Else
Action "is OK!"/bin/true
Fi
}
Web $
[Email protected] script]#
====================================================
[Email protected] script]# sh webjk.sh www.baidu.com
Www.baidu.com is OK! [OK]
[Email protected] script]# sh webjk.sh www.baidu.com1
WWW.BAIDU.COM1 is fail! [FAILED]
[Email protected] script]# sh webjk.sh t.tt
T.TT is OK! [OK]
[email protected] script]# cat webjk.sh
#####################################################
This article is from the "Struggle Bar" blog, please be sure to keep this source http://lvnian.blog.51cto.com/7155281/1701265
Simple usage of shell functions