作者:多伦多打折优惠信息_205 | 来源:互联网 | 2023-10-13 17:54
function
函数名 ()
{函数体;}**跨脚本调用函数**
source 写了函数的脚本
然后就可以在当前环境调用了
local 变量名 意义同局部变量
return 结束函数返回结果
$?可查看函数返回状态
function(){echo $[$1+$2]
}
function 10 20
调用函数直接,写函数名
小技巧 系统路径/etc/init.d/funcation 文件里带有大量系统函数
action 函数可实现以下功能
递归调用
不停调用自己 linux中会不停开启子shell 递归函数一定要有结束条件
func () {let i++echo $ifunc
}
函数库(写一些功能)
#!/bin/bash
COLOR="echo -e \E[1;32m"
END="\E[0m"
NUMBER="10" disable_firewalld(){${COLOR}********** stop the firewalld and close the firewalld *********${END}FIREWALLD_STATUS=`systemctl status firewalld`if echo ${FIREWALLD_STATUS} |grep -o "active (running)"; thensystemctl stop firewalld && systemctl disable firewalld &>/dev/null${COLOR}********** firewalld status *********${END}FIREWALLD_STATUS="firewalld is dead"echo ${FIREWALLD_STATUS}else${COLOR}********** firewalld status *********${END}FIREWALLD_STATUS="firewalld is dead"echo ${FIREWALLD_STATUS}fi${COLOR}********** firewalld over *********${END}
}disable_selinux(){sed -ir 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/configsetenforce 0echo "目前selinux的状态为`getenforce`"
}network_interface(){sed -r -i.bak '/^GRUB_CMDLINE_LINUX=/s@"$@ ifnames=0"@' /etc/default/grubSYSINFO=`cat /proc/version`if echo ${SYSINFO} | grep -oE 'centos' &> /dev/null ;thengrub2-mkconfig -o /boot/grub2/grub.cfgecho "centos 重启后生效"elif echo ${SYSINFO} | grep -oE 'ubuntu' &> /dev/null;thengrub-mkconfig -o /boot/grub/grub.cfgecho "ubuntu 重启后生效"
}infor_system(){RED="\E[1;31m"GREEN="echo -e \E[1;32m"END="\E[0m"$GREEN----------------------Host systeminfo--------------------$ENDecho -e "HOSTNAME: $RED`hostname`$END"echo -e "IPADDR: $RED` hostname -I`$END"echo -e "OSVERSION: $RED`cat /etc/redhat-release`$END"echo -e "KERNEL: $RED`uname -r`$END"echo -e "CPU: $RED`lscpu|grep 'Model name'|tr -s ' '|cut -d : -f2`$END"echo -e "MEMORY: $RED`free -h|grep Mem|tr -s ' ' : |cut -d : -f2`$END"echo -e "DISK: $RED`lsblk |grep '^sd' |tr -s ' ' |cut -d " " -f4`$END"$GREEN---------------------------------------------------------$END
}
回到主目录
首页