First, the function
1. What is the shell functions
The same definition of a function block, the code amount can be reduced throughout the program to improve development efficiency
Increase readability and legibility of the program, improve management efficiency
Failure can program features a modular, so that the program has portability
In fact, linux system which nearly 2,000 commands can be said is a function of the shell
2, Grammar
function name () compound command block [redirection]
function function name () {#function negligible written instructions. . . exit and return n # Similarly, return is a function of exit }
3, based on practice
1) Develop a build two simple function calls and execution
[root@web1 scripts]# cat test21.sh #!/bin/bash boy(){ echo "i am boy" } function girl(){ echo "i am girl" } boy girl [root@web1 scripts]# ./test21.sh i am boy i am girl [root@web1 scripts]# cat test21-1.sh #!/bin/bash boy(){ echo "i am boy" } function girl(){ echo "i am girl" } boy girl boy1 [root@web1 scripts]# ./test21-1.sh i am boy i am girl ./test21-1.sh: line 10: boy1: command not found [root@web1 scripts]#
2) and the number of execution script file line separation function body
[root@web1 scripts]# cat >>/etc/init.d/functions<<- EOF > boy(){ > echo "i am boy" > } > EOF [root@web1 scripts]# !tail tail -3 /etc/init.d/functions boy(){ echo "i am boy" } [root@web1 scripts]#
[root@web1 ~]# function boy1 { > echo "i am boy1" > }
[root@web1 scripts]# boy1
i am boy1
4, enterprise-class URL checking scripts
Participation by the mass function into a script file transfer command line parameters to determine whether there is any specified url removed
1) Practice script parameter passing, to check whether the normal web URL
[root @ web1 scripts] # CAT test23. SH # pass parameters to determine whether the format is 1 # ! / bin / bash IF [$ # -en 1 ] # "$ #" get back the total number of parameters of the currently executing script access the then echo $ " Usage: $ 0 url " Exit 1 fi wget --spider -q -o / dev / null --tries = 1 -T 5 $ 1 #T timeout practice, where $ 1 is the parameter script IF [$? -eq 0 ] # "$ ?" get on a return instruction execution status value, 0 on success, nonzero failure the then echo " $ 1 iS yes " else echo "$1 is no" fi
2) The above detection function written as a function, the function and parameter passing is converted into a script transmission command line parameters, determines whether or not any abnormality specified URL
! # / bin / the bash function Usage () { echo $ " Usage: $ 0 URL " Exit . 1 } function check_url () { # URL detection function wget --spider -q -o / dev / = Full --tries . 1 -T 5 $ 1 # this is the function parameter passing $ 1 if [$? -eq 0 ] the then echo " $ 1 iS Yes " the else echo " $ 1 iS NO " Fi } function main () { # main function if[$ # -Ne . 1 ] # If a plurality of parameters passed, print help function, the user prompts the then Usage Fi check_url $ . 1 } # receiving transfer function parameters, i.e. below the end of the main $ * cominghere main $ * # $ * here is to put all the parameters as a function of the received command line parameter to an internal function, it is a common transceiver
operation result
[root@web1 scripts]# sh test24.sh www.baidu.com www.baidu.com is yes [root@web1 scripts]# sh test24.sh www.baidu1.com www.baidu1.com is no [root@web1 scripts]#
3) The transfer function of the reference conversion layer script file transfer command line parameters, determined arbitrarily designated url whether there is abnormality, and is displayed in more specialized
! # / bin / SH . /etc/init.d/ Functions function Usage () { echo $ " Usage: $ 0 URL " Exit . 1 } function check_url () { wget --spider -q -o / dev / null - = tries 1 -T 5 $ 1 IF [$? -eq 0 ] the then Action " $ 1 iS yes. " / bin / to true #action is to call the library functions the else Action " $ 1 iS NO. " / bin /false fi } function main(){ if [ $# -ne 1 ] then usage fi check_url $1 } main $*
effect
[root@web1 scripts]# chmod +x test25.sh [root@web1 scripts]# ./test25.sh www.baidu.com www.baidu.com is yes. [ OK ] [root@web1 scripts]# ./test25.sh www.baidu1.com www.baidu1.com is no. [FAILED] [root@web1 scripts]#
5, the use of shell functions developed a key optimization system script
centos6
#!/bin/bash # author:oldboy # qq:31333741 #set env export PATH=$PATH:/bin:/sbin:/usr/sbin # Require root to run this script. if [ "$UID" != "0" ]; then echo "Please run this script by root." exit 1 fi #define cmd var SERVICE=`which service` CHKCONFIG=`which chkconfig` function mod_yum(){ #modify yum path if [ -e /etc/yum.repos.d/CentOS-Base.repo ] then mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup&&\ wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo fi } function close_selinux(){ #1.close selinux sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config #grep SELINUX=disabled /etc/selinux/config setenforce 0 &>/dev/null #getenforce } function close_iptables(){ #2.close iptables /etc/init.d/iptables stop /etc/init.d/iptables stop chkconfig iptables off } function least_service(){ #3.least service startup chkconfig|awk '{print "chkconfig",$1,"off"}'|bash chkconfig|egrep "crond|sshd|network|rsyslog|sysstat"|awk '{print "chkconfig",$1,"on"}'|bash #export LANG=en #chkconfig --list|grep 3:on } function adduser(){ #4.add oldboy and sudo if [ `grep -w oldboy /etc/passwd|wc -l` -lt 1 ] then useradd oldboy echo 123456|passwd --stdin oldboy \cp /etc/sudoers /etc/sudoers.ori echo "oldboy ALL=(ALL) NOPASSWD: ALL " >>/etc/sudoers tail -1 /etc/sudoers visudo -c &>/dev/null fi } function charset(){ #5.charset config cp /etc/sysconfig/i18n /etc/sysconfig/i18n.ori echo 'LANG="zh_CN.UTF-8"' >/etc/sysconfig/i18n source /etc/sysconfig/i18n #echo $LANG } function time_sync(){ #6.time sync. cron=/var/spool/cron/root if [ `grep -w "ntpdate" $cron|wc -l` -lt 1 ] then echo '#time sync by oldboy at 2010-2-1' >>$cron echo '*/5 * * * * /usr/sbin/ntpdate time.nist.gov >/dev/null 2>&1' >>$cron crontab -l fi } function com_line_set(){ #7.command set. if [ `egrep "TMOUT|HISTSIZE|HISTFILESIZE" /etc/profile|wc -l` -ge 3 ] then echo 'export TMOUT=300' >>/etc/profile echo 'export HISTSIZE=5' >>/etc/profile echo 'export HISTFILESIZE=5' >>/etc/profile . /etc/profile fi } function open_file_set(){ #8.increase open file. if [ `grep 65535 /etc/security/limits.conf|wc -l` -lt 1 ] then echo '* - nofile 65535 ' >>/etc/security/limits.conf tail -1 /etc/security/limits.conf fi } function set_kernel(){ #9.kernel set. if [ `grep kernel_flag /etc/sysctl.conf|wc -l` -lt 1 ] then cat >>/etc/sysctl.conf<<EOF #kernel_flag net.ipv4.tcp_fin_timeout = 2 net.ipv4.tcp_tw_reuse = 1 net.ipv4.tcp_tw_recycle = 1 net.ipv4.tcp_syncookies = 1 net.ipv4.tcp_keepalive_time = 600 net.ipv4.ip_local_port_range = 4000 65000 net.ipv4.tcp_max_syn_backlog = 16384 net.ipv4.tcp_max_tw_buckets = 36000 net.ipv4.route.gc_timeout = 100 net.ipv4.tcp_syn_retries = 1 net.ipv4.tcp_synack_retries = 1 net.core.somaxconn = 16384 net.core.netdev_max_backlog = 16384 net.ipv4.tcp_max_orphans = 16384 net.nf_conntrack_max = 25000000 net.netfilter.nf_conntrack_max = 25000000 net.netfilter.nf_conntrack_tcp_timeout_established = 180 net.netfilter.nf_conntrack_tcp_timeout_time_wait = 120 net.netfilter.nf_conntrack_tcp_timeout_close_wait = 60 net.netfilter.nf_conntrack_tcp_timeout_fin_wait = 120 EOF sysctl -p fi } function init_ssh(){ \cp /etc/ssh/sshd_config /etc/ssh/sshd_config.`date +"%Y-%m-%d_%H-%M-%S"` #sed -i 's%#Port 22%Port 52113%' /etc/ssh/sshd_config sed -i 's%#PermitRootLogin yes%PermitRootLogin no%' /etc/ssh/sshd_config sed -i 's%#PermitEmptyPasswords no%PermitEmptyPasswords no%' /etc/ssh/sshd_config sed -i 's%#UseDNS yes%UseDNS no%' /etc/ssh/sshd_config /etc/init.d/sshd reload &>/dev/null } function update_linux(){ #10.upgrade linux. if [ `rpm -qa lrzsz nmap tree dos2unix nc|wc -l` -le 3 ] then yum install lrzsz nmap tree dos2unix nc -y #yum update -y fi } main(){ mod_yum close_selinux close_iptables least_service adduser charset time_sync com_line_set open_file_set set_kernel init_ssh update_linux } main
centos7
not up-to-date
6, the use of shell functions developed rsync service startup script
#!/bin/sh if [ $# -ne 1 ] then echo $"usage:$0{start|stop|restart}" exit 1 fi if [ "$1" = "start" ] then rsync --daemon if [ `netstat -lntup|grep rsync|wc -l` -ge 1 ] then echo "rsyncd is started." exit 0 fi elif [ "$1" = "stop" ] then pkill rsync if [ `netstat -lntup|grep rsync|wc -l` -eq 0 ] then echo "rsyncd is stopped." exit 0 fi elif [ "$1" = "restart" ] then pkill rsync sleep 2 rsync --daemon else echo $"usage:$0{start|stop|restart}" exit 1 fi
Reference to learn: https://blog.51cto.com/oldboy/1855639