functions: Function functions are statement blocks consisting of several shell commands for code reuse and modular programming
1) Definition of function:Can be defined in the command line, part of the script, only in a separate file of the function
Two parts: function name and function body; functions can have return status value returned #
-Syntax One:
function F_name {
... function Body ...
}
-Syntax Two:
F_name () {
... function Body ...
}
Define and invoke functions at the command line
] #dir () {ls;}] #dir
2) Call to function:function is only executed if it is called
Given function name:
No parameter call F_name
There are parameters called F_name arg1 arg2 ...
When the function is called, it is created and terminates on return.
Command line: After defining a function, you can call directly using the #unset f_name undo function
In script: function is defined before it can be used, the function should be placed at the beginning of the script
Function file: Writes a common function to a function file, and loads the function file in the command line or script to invoke the function
#set View all loaded functions #unset delete functions
Load function:
Source/path/to/filename
. /path/to/filename
Load function File
] #vi Echo.sh#!/bin/bash#zjjprint_haha () {echo haha}] #source echo.sh] #print_haha] #haha
3) Function parameters
Pass parameters to the function:
F_name arg1 arg2.
The function body can use $1,$2,... Call the corresponding parameter; [Email protected],$*,$ #等同样可用
4) function variables Note that the operation of the variables in the function body affects the entire script,
can define local variables in the function. Valid only for function body, use LOCAL
local var_name= VALUE
5) Recursive
function directly or indirectly calls itself
note the number of recursive layers
=== Function Exercises:
1. Write a service script/root/bin/testsrv.sh
#!/bin/bash#zjjtestsrv () { bsname= ' basename $0 ' local srvpath=/var/lock/subsys/$bsname case $1 in start) if [ -e $Srvpath ] ;then echo + $bsname is running else touch $Srvpath & & echo ++ start successful fi ;; stop) if [ -e $Srvpath ] ;then rm -f $Srvpath &&echo -- stop successful else echo - $bsname is dead fi ;; restart) testsrv stop && testsrv start ;; status) [ -e $Srvpath ] &&echo + $bsname is running... | | echo - $bsname is stopped... ;; *) testsrv status ;; esac }testsrv $1
2. Write a script/root/bin/copycmd.sh, copy the command and its library file
#!/bin/bash#zjjcopycmd () {# create directiry if they does not exist local dpath= '/testdir/sysroot ' for dir in ' echo $PATH |tr ': ' ' ' '/lib ' '/lib64 ' ;d o [ -e ${dpath}${dir} ] &&continue | | mkdir -p ${dpath}${dir} done while true ;d o read -p "++enter a command or [quit]:" cmd [[ $cmd =~ quit ]] &&exit 23 Spath= ' whereis -b $cmd 2>/dev/null|cut -d ' ' -f2 ' ! [ $? -eq 0 ] && echo invalid command & &continue cp -a $spath ${dpath} $spath &&echo copy $spath ++succesful | | echo --error for so in ' ldd $spath |sed -nr ' s/.*> ([^[:space:]].*) \ (. */\1/p ";d o cp -n $so ${dpath} $so &&echo -e "copy $so \t++successful " | | Echo --error donedone}copycmd
3. write a function to implement two numbers as parameters, return the maximum value
#!/bin/bash#zjjmax () {[$1-gt $]&&echo max:$1| | Echo Max:$2}max $ A
4. Fibonacci Sequence
#!/bin/bash#zjjfibo () { if [ $1 -le 1 ];then echo $1 elif [ $1 -gt 1 ];then echo "$[$ (Fibo $[$1-1]) +$ (Fibo $[$1-2])]" fi}if ! [ $1 -eq $1 ]&>/dev/null| | [ $1 -lt 0 ]&>/dev/null| | [ -z $1 ] ;then echo please enter a integer &&exit 23else for ((i=0;i<=$1;i++));d o fibo $i donefi
5. Hanoi Tower Solution
#!/bin/bash#zjj# move n-1: A to b# move n:a to C # move n-1: B to Chanoi () {if [$1-eq 1];then echo $ t o $4 else Hanoi $[$1-1] $ $4 $ echo $ to $4 Hanoi $[$1-1] $ $ $4 fi}hanoi $ A B C
This article is from the "mediocre" blog, please be sure to keep this source http://zzjasper.blog.51cto.com/9781564/1840791
Function of Shell programming