1. Functions
Shell functions, which are used to customize a segment of a program. To simplify the code. Grammar:
Funtion fname () { //do something}
The shell is top-down, executed from the left and right. So the definition of the function needs to precede the call. For example:
#!/bin/bash# author:yonggangfunction print_it () { echo-n "Your choice is:"}case $ in "one") print_it; EC Ho $;; " ") Print_it; echo $;;; " Three ") Print_it, Echo $;;; *) echo "Usage (One|two|three)" ;; Esac
Perform:
[[Email protected] sh]$ sh func.sh twoyour choice is:two[[email protected] sh]$ sh func.sh oneyour Choice Is:one[[emai L Protected] sh]$
2. function parameter passing
function also has built-in variables, similar to shell script. $# parameter number of the first parameter, the second argument ... [email protected] All parameters see the following example:
#!/bin/bash# author:yonggangfunction Print_param () { echo "paramter number:" $# echo "First paramter:" $1
echo "Second paramter:" $ echo "all paramter:" [email Protected]}print_param One and three
Run:
[[Email protected] sh]$ sh func.sh paramter number: 3first paramter: onesecond paramter: twoall paramter:
Address: http://blog.csdn.net/yonggang7/article/details/40679111
Shell function function