Linux基础之shell--函数(长期更新函数)

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
}

回到主目录

首页
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

霖宇长清

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值