Bash supports the use of functions, where functions appear, and is automatically replaced with function-defined code, which can be reused multiple times after a function definition, greatly reducing the amount of code
function definition Format:
First Kind
FuncName () {function Body}
The second Kind
function FuncName {functional body}
The function has two return values:
Data returned as normal:
A print statement in a function, such as echo or print
Execution result of the command in the function
Execution status return value:
Depends on the last statement executed in the function, the last execution successfully returns 0, no just returned to other
Inverse value can be customized: use return N # N for custom return values
The function can accept parameters:
The function body can use similar parameters like a script to call positional parameters
$, $, ...
$#
$*, [email protected]
What if you want to pass all the positional parameters of the script to a function used in the script?
Use $* to pass, if you want to use a one, then judge $ #有几个, and then cycle one of the use
If a variable is used in a function: variable scope
Variables declared in the main program are used in the function, and the re-assignment will directly modify the variables in the main program;
If you do not expect the function to conflict with a variable in the main program, the variables used in the function are decorated with local, even with local variables;
A variable that is not declared in the main program is used in the function and is undone after the execution of the function, regardless of whether the local modifier is used or not;
Examples of applications:
Write a script to complete the following functions
1. Display the following menu
disk) Show Disk info
MEM) Show Memory info
CPU) Show Cpuinfo
2, display the user selected content;
#!/bin/bash#showmenu () { ## first method of defining function names Cat << eofdisk) show &NBSP;DISK&NBSP;INFOMEM) (&NBSP;SHOW&NBSP;MEMORY&NBSP;INFOCPU) show cpuinfoquit) exit scripteof}function main { ## the second method of calling a function while true ;d oshowmenu ## calls the defined function, the function inline function. read -p "please enter menu option: " optionoption= ' echo $option | tr ' A-Z ' ' A-Z ' case $option in disk) df -h ;; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;MEM) free -m ;; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;CPU) cat /proc/cpuinfo ;; quit) exit 0 ;; *) echo "error option." esacdone}main ## calling functions
Write a script to determine which hosts in the 172.16.0.0 network are online, with green display on the line, not in red on the lines, requirements, function in programming;
#!/bin/bash#cnetping () { for i in {1..254};d o ping -c 1 -w 1 $1. $i done}# cnetping 192.168.1.= 192.168.1+$1bnetping () { for j in {0..255};d o cnetping $1. $j done}# bnetping 172.16. = 172.16+ $j + $ianetping () { for k in {0..255};d o bnetping $1. $k done}# anetping 10.=10. $k + $j + $iread -p "enter ipaddr: " ipaddrnettype= ' echo $IPaddr | cut -d '. ' -f1 ' if [ $NetType -ge 0 -a $NetType -le 127 ]; then anetping ' echo $IPaddr | awk -f '. ' ' {print $1} ' elif [ $NetType -ge 128 -a $NetType -le 191 ];then bnetping ' echo $IPaddr &Nbsp;| awk -f '. ' ' {print $1 '. " $ "elif [ $NetType -ge 192 -a $NetType -le 223 ]; then cnetping ' echo $IPaddr | awk -f '. ' ' {print $1 '. " $ "." $ else echo "Error ip" fi
Write a script that accomplishes the following functions (using functions):
1, prompt the user to enter an executable command;
2. Get all the library files that this command relies on (using the LDD command);
3, copy the command to the/mnt/sysroot/corresponding directory
Explanation: If you are replicating a cat command whose executable path is/bin/cat, copy the/bin/cat to the/mnt/sysroot/bin/directory, and if the Useradd command is copied, and the path to the Useradd is/ Usr/sbin/useradd, then copy it to the/mnt/sysroot/usr/sbin/directory;
4, copy the library files to the/mnt/sysroot/corresponding directory, the requirements of the order;
#!/bin/bash#copytarget=/mnt/sysroot[ -d $copytarget ] | | mkdir -p $copytargetcommand () {if which $COMMAND &> /dev /null;then command= ' which --skip-alias $COMMAND ' else return 5fi}copycommand () { command_dir=${ Copytarget} ' dirname $COMMAND ' [ -d ${command_dir} ] | | mkdir -p ${command_dir} [ -f ${copytarget}${command} ] | | cp $COMMAND ${command_dir}}copylib () { for i in ' ldd $ command | grep -o "/[^[:space:]]\{1,\}" ';d O libdir=${copytarget} ' dirname $ I ' [ -d $libdir ] | | mkdir $libdir [ -f ${copytarget}${i} ] | | cp $i $libdir done}while true;doread -p "please enter command: " COMMAND[ " $COMMAND " == ' Quit ' ] && exit 7 command[ $? -eq 5 ] && continue copycommand copylibdone
Write a script that accomplishes the following functions (using functions):
1, the script uses the format:
mkscript.sh [-d|--description "script description"] [-a|--author "script author"]/path/to/somefile
2. If the file does not exist beforehand, it is created, and the first few lines are as follows:
#!/bin/bash
# Description:script Description
# Author:script Author
#
3, if pre-existing, but not empty, and the first line is not "#!/bin/bash", then the error and exit; If the first line is "#!/bin/bash", use vim to open the script; position the cursor directly to the last line
4. When the script is turned off, determine if the script has a syntax error
If there is, prompt to enter Y to continue editing, enter N to discard and exit;
If not, give this file permission to execute;
#!/bin/bash# #mkscript .sh [-d|--description "script description"] [-a|--author "script author "] /path/to/somefilewhile true;do[ $# -lt 1 ] && Breakcase $1 in -d|--description) description=$2 shift 2 ;; -a|--author) author=$2 shift 2 ;; *) file=$1 shift 1esacdone[ -d ' dirname $file ' ] | | mkdir ' dirname $file ' if ! [ -e $file ];then echo -e "#!/bin/bash\n# description:${description}\n# author:${author}\n#" > $fileelif [ -f $file ];then head -1 $file | grep ' #!/bin/ Bash ' &> /dev/null && vim +$ $fileelse echo " error $file not scripts file."; exit 7fi## Check script while true;doif bash -n $file &> / dev/null;then chmod +x $file breakfiread -p "enter y contiue ,n exit script!" option case $option in y| Y) vim $file ;; n| N) exit 8 ;; *) echo "Error option" esacdone
This article is from "Rookie Diary" blog, please make sure to keep this source http://zkxfoo.blog.51cto.com/1605971/1757295
bash function used for modular programming