Shell 函数详解(函数定义、函数调用)

Shell 函数的本质是一段可以重复使用的脚本代码,这段代码被提前编写好了,放在了指定的位置,使用时直接调取即可。

Shell 中的函数和C++JavaPythonC# 等其它编程语言中的函数类似,只是在语法细节有所差别。

Shell 函数定义的语法格式如下:

bash 复制代码
function name() {
    statements
    [return value]
}

对各个部分的说明:

  • function是 Shell 中的关键字,专门用来定义函数
  • name是函数名
  • statements是函数要执行的代码,也就是一组语句
  • return value表示函数的返回值,其中 return 是 Shell 关键字,专门用在函数中返回一个值;这一部分可以写也可以不写。

{ }包围的部分称为函数体,调用一个函数,实际上就是执行函数体中的代码。

函数定义的简化写法


如果你嫌麻烦,函数定义时也可以不写 function 关键字:

bash 复制代码
name() {
    statements
    [return value]
}

如果写了 function 关键字,也可以省略函数名后面的小括号:

bash 复制代码
function name {
    statements
    [return value]
}

我建议使用标准的写法,这样能够做到"见名知意",一看就懂。

函数调用


调用 Shell 函数时可以给它传递参数,也可以不传递。如果不传递参数,直接给出函数名字即可:

bash 复制代码
name

如果传递参数,那么多个参数之间以空格分隔:

bash 复制代码
name param1 param2 param3

不管是哪种形式,函数名字后面都不需要带括号。

和其它编程语言不同的是,Shell 函数在定义时不能指明参数,但是在调用时却可以传递参数,并且给它传递什么参数它就接收什么参数。

Shell 也不限制定义和调用的顺序,你可以将定义放在调用的前面,也可以反过来,将定义放在调用的后面。

实例演示


  1. 定义一个函数,输出 Shell 教程的地址:
bash 复制代码
#!/bin/bash

#函数定义
function url(){
    echo "http://c.biancheng.net/shell/"
}

#函数调用
url

运行结果:

http://c.biancheng.net/shell/

你可以将调用放在定义的前面,也就是写成下面的形式:

bash 复制代码
#!/bin/bash
#函数调用
url

#函数定义
function url(){
    echo "http://c.biancheng.net/shell/"
}
  1. 定义一个函数,计算所有参数的和:
bash 复制代码
#!/bin/bash
function getsum(){
    local sum=0
    for n in $@
    do
         ((sum+=n))
    done
    return $sum
}
getsum 10 20 55 15  #调用函数并传递参数
echo $?

运行结果:

100

@表示函数的所有参数,?表示函数的退出状态(返回值)。关于如何获取函数的参数,我们将在《Shell函数参数》一节中详细讲解。

此处我们借助 return 关键字将所有数字的和返回,并使用$?得到这个值,这种处理方案在其它编程语言中没有任何问题,但是在 Shell 中是非常错误的,Shell 函数的返回值和其它编程语言大有不同,我们将在《Shell函数返回值》中展开讨论。

示例 系统初始化


此脚本用于新装Linux的相关配置工作,比如更换默认yum源,优化系统内核、停掉一些没必要启动的系统服务等。此脚本尤其适合大批新安装的CentOS系列的服务器。适用于Centos7

bash 复制代码
shell>vim cenots_7_system_init.sh
#!/bin/bash
# Filename:    centos7-init.sh
# Author:      test
 
#判断是否为root用户
if [ `whoami` != "root" ];then
echo " only root can run it"
exit 1
fi
 
#执行前提示
echo -e "\033[31m 这是centos7系统初始化脚本,将更新系统内核至最新版本,请慎重运行!\033[0m" 
read -s -n1 -p "Press any key to continue or ctrl+C to cancel"
echo "Your inputs: $REPLY"
 
#1.定义配置yum源的函数
yum_config(){
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-7.repo
yum clean all && yum makecache
}
 
#2.定义配置NTP的函数
ntp_config(){
yum --y install chrony
systemctl start chronyd && systemctl enable chronyd
timedatectl set-timezone Asia/Shanghai && timedatectl set-ntp yes
}
 
#3.定义关闭防火墙的函数
close_firewalld(){
systemctl stop firewalld.service &> /dev/null 
systemctl disable firewalld.service &> /dev/null
}
 
#4.定义关闭selinux的函数
close_selinux(){
setenforce 0
sed -i 's/enforcing/disabled/g' /etc/selinux/config
}
 
#5.定义安装常用工具的函数
yum_tools(){
yum install --y vim wget curl curl-devel bash-completion lsof iotop iostat unzip bzip2 bzip2-devel
yum install --y gcc gcc-c++ make cmake autoconf openssl-devel openssl-perl net-tools
source /usr/share/bash-completion/bash_completion
}
 
#6.定义升级最新内核的函数
update_kernel (){
rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org
rpm -Uvh http://www.elrepo.org/elrepo-release-7.0-3.el7.elrepo.noarch.rpm
yum --enablerepo=elrepo-kernel install -y kernel-ml
grub2-set-default 0
grub2-mkconfig -o /boot/grub2/grub.cfg
}
 
#执行脚本
main(){
    yum_config;
    ntp_config;
    close_firewalld;
    close_selinux;
    yum_tools;
    update_kernel;
}
main
相关推荐
yanlaifan2 天前
Shell编程---shell bash中的keyword
shell
柒号华仔2 天前
「速通Shell」常用命令(下)—— 归档、权限与进阶工具
shell
柒号华仔3 天前
「速通Shell」常用命令(中)——进程、网络与系统信息
shell
YuSun_WK3 天前
shell脚本
shell·脚本
柒号华仔7 天前
「速通Shell」Shell 脚本测试详解
shell
柒号华仔8 天前
「速通Shell」Shell 脚本模块化
shell
柒号华仔9 天前
「速通Shell」Shell编程的错误处理与日志
shell
柒号华仔10 天前
「速通Shell」Shell 数组、关联数组与 mapfile
shell
苏灿烤鱼12 天前
一套进程代替八件套,桌面更稳还是单点更大
linux·github·shell
茶本无香12 天前
通用报表自动化框架:Java调用Shell传参执行PostgreSQL SQL模板
java·sql·postgresql·shell