Shell-06 function
#Write a script, use the chkconfig command, loop execution, and close all 5 levels of services
#!/bin/bash
name=`chkconfig --list | cut -d' span> ' -f1`
for i in [name];do
chkconfig --level 5 $i off
done
name=`chkconfig --list | awk ' {print $1}'`
for i in $name;do
if chkconfig --list $i | grep "5:off" &> /dev/null;then
echo " This $i service has been closed"
else
chkconfig --level 5 $i off
echo "This $i service has been closed"
fi
done
1. Function introduction
In order to avoid For code reuse, we generally write code blocks through functions, and this code block is used to achieve a certain function. And this function will be called repeatedly in the code later;
Def
2. Function format
function
Format 1:
Function fun_name{
Function body
< p align="left">}
Fun_name()
[If the function is not called, the command in the function body will not be Executed; where it is called, the command in the function body is executed there]
Format 2:
Fun_name(){
p>
Function body
$1 $2…$10
}
Fun_name 1 2 3
:wq
Fun_name 4 5 6 7 8 9 10 11 12 13 14
< p align="left">
1.Case PATTERN supported format: * Any content start|stop) a*)
Case The pattern in the statement supports file wildcards
2.*)
Echo “Usage: please input{start|stop|restart|status }”
- start)
if [-e /var/lock/subsys/testd_name] ;then
< p align="left"> echo “Foot service has been activated”
else
touch /var/lock/subsys/testd_name
echo “Service started successfully”
fi
- stop)
if [-e /var/lock/subsys/testd_name] ;then p>
rm -f /var/lock/subsys/testd_name
echo “Service hook closed successfully”
else
echo “Service is closed”
fi
- restart)
[Write the previous code as a function]
Start
Stop
- status
fun_start(){
If [-e /var/lock/subsys/testd_name ];then
echo “service started”
else
touch /var/lock/subsys/testd_name
echo “Service started successfully”
fi< /p>
}
fun_stop(){
if [-e /var/lock/subsys/testd_name ];then
rm -f /var/lock/subsys/testd_name
echo “service closed successfully”
else
echo “service closed”
fi
}
fun_restart(){
fun_stop
fun_start
}
fun_status(){
if [-e /var/lock/subsys/testd_name ];then
echo “testd_name is running”
else
echo “testd_name is stopped”
fi
}
fun_usage( ){
echo “Usage: please input{start|stop|restart|status}”
}
if [$# -lt 1 ];then
Usage
e xit 1
fi
case $1 in
start)
fun_start
;;
stop)
fun
p>
;;
reatart)
fun_restart
status)
fun_status
fun_status
align=”left”> *)
fun_usage
fun_usage
;
esac
p>
- Function return is worth the problem
useradd_1(){
for i in `seq 1 10` ;do
if id user$i &> /dev/null;then
echo "user$i exists"
else
useradd user$i &> /dev/null
echo "user$i OK"
return 0
fi
done
}
useradd_1
a) When a judgment statement is specified in a function, an error result is returned by default. But the last command is indeed correct, and we need to use return to return the correct status code we need.
b) When the function ends, we need to specify a correct return code for return;
If a loop statement is used, return will jump out Cycle
Userdel -r delete user and user’s home directory
- The function receives parameters
Function variables
Same as ordinary commands
Just note that when receiving parameters, external parameters will not be used, and It is based on the parameters inside the script
- Function variables
Local variables
Local variable name=xxx
Local variables only take effect inside this function
Local variables
The variables inside the script
Environmental variables
Effective in the bash of the entire Linux system
i=1
TEST(){
Local i=9
Let i++
Echo i
Exit 0
}
TEST
Echo i
Extension: Questions about script execution
a) bash way to execute
The variables defined in the script executed by bash, in the current shell In the shell
b) Execute through the full path or relative path——-#!/bin/bash requires authorization to execute permissions;
c) Source script file or .Script file
When the script is executed by Source this way, the value defined by the internal variable is in the current shell
- Function recursion
You stand in front of the mirror, and there is a mirror behind
n’s factorial
n!=n*(n-1)!
(n-1)!=(n-1)*(n-2)!
fun(){
if [$1 -le 1 ];then
echo 1
else
q=$[$1*$(fun$[$1 -1])]
echo $q
fi
}
fun 5
< /p>
Rabbit sequence, Fibonacci sequence
1+1=2 1+ 2=3 2+3=5 3+5=8
1 1 2 3 5 8 13 21 34….
Fn = F(n-1)+F(n-2)
#!/bin/bash
name=`chkconfig --list | cut -d' span> ' -f1`
for i in [name];do
chkconfig --level 5 $i off
done
name=`chkconfig --list | awk ' {print $1}'`
for i in $name;do
if chkconfig --list $i | grep "5:off" &> /dev/null;then
echo " This $i service has been closed"
else
chkconfig --level 5 $i off
echo "This $i service has been closed"
fi
done
useradd_1() {
for i in `seq 1 10` ;do
if id user$i &> /dev/null;then
echo "user$i exists"
else
useradd user$i &> /dev/null
echo "user$i OK"
return 0
fi
done
}
useradd_1
n’s factorial
n!=n*(n-1)!
(n-1)!=(n-1)*(n-2)!
fun(){
if [$1 -le 1 ];then
echo 1
else
q=$[$1*$(fun$[$1 -1])]
echo $q
fi
}
fun 5