function
函数名 ()
{
函数体;
}
**跨脚本调用函数**
source 写了函数的脚本
然后就可以在当前环境调用了
local 变量名 意义同局部变量
return 结束函数返回结果
$?可查看函数返回状态
function(){
echo $[$1+$2]
}
function 10 20 #传参
调用函数直接,写函数名
小技巧 系统路径/etc/init.d/funcation 文件里带有大量系统函数
action 函数可实现以下功能
递归调用
不停调用自己 linux中会不停开启子shell 递归函数一定要有结束条件
#轻松破坏linux
func () {
let i++
echo $i
func
}
函数库(写一些功能)
#!/bin/bash
#
#********************************************************************
#Author: YiMingLANG
#QQ: 1428040096
#Date: 2021-10-4
#FileName: founcations
#Copyright (C): 2021 All rights reserved
#********************************************************************
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)"; then
systemctl 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/config
setenforce 0
echo "目前selinux的状态为`getenforce`"
}
network_interface(){
sed -r -i.bak '/^GRUB_CMDLINE_LINUX=/s@"$@ ifnames=0"@' /etc/default/grub
#此处替换配置文件(错误删除,需要补充)
SYSINFO=`cat /proc/version`
if echo ${SYSINFO} | grep -oE 'centos' &> /dev/null ;then
#centos
grub2-mkconfig -o /boot/grub2/grub.cfg
echo "centos 重启后生效"
elif echo ${SYSINFO} | grep -oE 'ubuntu' &> /dev/null;then
#ubuntu
grub-mkconfig -o /boot/grub/grub.cfg
echo "ubuntu 重启后生效"
}
infor_system(){
RED="\E[1;31m"
GREEN="echo -e \E[1;32m"
END="\E[0m"
$GREEN----------------------Host systeminfo--------------------$END
echo -e "HOSTNAME: $RED`hostname`$END"
#echo -e "IPADDR: $RED` ifconfig eth0|grep -Eo '([0-9]{1,3}\.){3}[0-9]{1,3}' |head -n1`$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
}