运维Shell脚本小试牛刀(一)
运维Shell脚本小试牛刀(二)
运维Shell脚本小试牛刀(三)::$(cd $(dirname $0); pwd)命令详解
运维Shell脚本小试牛刀(四): 多层嵌套if...elif...elif....else fi_蜗牛杨哥的博客-CSDN博客
Cenos7安装小火车程序动画
运维Shell脚本小试牛刀(五):until循环
运维Shell脚本小试牛刀(六): Shell中的函数认知
一: Shell中函数认知
shell中定义函数,可以更加方便的实现模块化, 函数是一系列命令的集合堆砌实现某些功能的代码块, Shell中定义函数的语法:
1.1:定义函数方法
function function_name() {
command;
command2
command3
[ return int; ] # 参数返回,return 语句时可选择的。如果没有return 语句,则以函数最后一 条命令的运行结果作为返回值;如果使用return 语句, 则返回return 后跟数值n (数值范 围: 0~255)
}
注意: 如果用function关键字,可以省略括号"()"。函数体,符合命令块,是包含在{}大括号之间的命令列表;也可以在一行内定义函数,此时,函数体内的各命令之间必须用分号";"隔开
function name { command1; command2; command3; }
或者
name() { command1; command2; command3; }
1.2: 语法糖式定义函数
function_name() {
command;
command2
command3
[ return int; ] # 参数返回,return 语句时可选择的。如果没有return 语句,则以函数最后一 条命令的运行结果作为返回值;如果使用return 语句, 则返回return 后跟数值n (数值范围: 0~255)
}
二:Shell脚本中的变量全局的,使用local限定变量范围
用户Shell中的变量是全局的,所以在函数中修改同名的变量会,修改Shell脚本中的同名变量的值,因此,为避免参数不必要的错误, 可以使用local关键字来修饰;且local只能在函数中使用,其修饰变量的函数的作用范围只在函数内;
[root@www functiondic]# cat locatparamfunction.sh
#!/bin/bash -
#==================================================================================================================
#
#
# FILE: localparamfunction.sh
# USAGE: ./localparamfunction.sh
# DESCRIPTION: shell脚本中变量都是全局变量,在函数中修改该变量会修改脚本中的变量,这在某些情况下可能会产生问题
# OPTIONS: -------
# REQUIREMENTS: ---------
#
# BUGS: ------
# NOTES: --------
# AUTHOR: ---------YangGe (TOM) ,YangGe.freedom@icloud.com
# ORGANIZATION:
# CREATED: 2023-8-24 09:11:20
# REVISION: --------
#
#
#
#
#
#====================================================================================================================
# 定义函数create_logFile
function create_logFile(){
# 修改变量d的值
d=$1
echo "create_logFile(): d is set to $d"
echo "=======================================Before create_logFile():==========================================="}
# 定义变量d
d=/usr/local/example/logs/zookeeper.logecho "Before calling create_logFile() d is set to $d"
# 调用函数后,create_logFile(),并指定一个参数
create_logFile "/usr/local/example/logs/diskUsage.log"echo "=========================================After create_logFile():================================================"
echo "After calling create_logFile(): d is set to $d"
2.1:测试Shell脚本中与函数中命名变量
[root@www functiondic]# ./locatparamfunction.sh
Before calling create_logFile() d is set to /usr/local/example/logs/zookeeper.log
create_logFile(): d is set to /usr/local/example/logs/diskUsage.log
=======================================Before create_logFile():===========================================
=========================================After create_logFile():================================================
After calling create_logFile(): d is set to /usr/local/example/logs/diskUsage.log
三: local修饰本地变量
[root@www functiondic]# cat localsetparamfunction.sh
#!/bin/bash -
#==================================================================================================================
#
#
# FILE: localparamfunction.sh
# USAGE: ./localparamfunction.sh
# DESCRIPTION: shell脚本中变量都是全局变量,在函数中修改该变量会修改脚本中的变量,这在某些情况下可能会产生问题,local变量只能在函数中使用,被该关键字修饰的变量指定作用范围只属于该函数本身
# OPTIONS: -------
# REQUIREMENTS: ---------
#
# BUGS: ------
# NOTES: --------
# AUTHOR: ---------YangGe (TOM) ,YangGe.freedom@icloud.com
# ORGANIZATION:
# CREATED: 2023-8-24 09:11:20
# REVISION: --------
#
#
#
#
#
#====================================================================================================================
# 定义全局变量
dic=/usr/local/example/logs/zookeeper.log# 定义一个函数create_log_local()
create_log_local(){
# 定义一个本地变量dic
local dic=$1
echo "create_log_local(): dic is set to $dic"
}echo "Before calling create_log_local() dic is set to $dic"
echo "=====================Before create_log_local()==============================================="
# 调用函数create_log_local(): 并指定一个参数
create_log_local /etc/yes/*
echo "====================After create_log_local()==================================================="
echo "After calling create_log_local(): dic is set to $dic"
echo "====================After create_log_local()==================================================="
3.1: 测试local修饰的本地变量
[root@www functiondic]# ./localsetparamfunction.sh
Before calling create_log_local() dic is set to /usr/local/example/logs/zookeeper.log
=====================Before create_log_local()===============================================
create_log_local(): dic is set to /etc/yes/*
====================After create_log_local()===================================================
After calling create_log_local(): dic is set to /usr/local/example/logs/zookeeper.log
====================After create_log_local()===================================================
四: 函数的返回值
4.1 使用函数的返回值
[root@www functiondic]# cat functionreturnvalue.sh
#!/bin/bash -
#==================================================================================================================
#
#
# FILE: localparamfunction.sh
# USAGE: ./localparamfunction.sh
# DESCRIPTION: shell脚本中函数的返回值测试
# OPTIONS: -------
# REQUIREMENTS: ---------
#
# BUGS: ------
# NOTES: --------
# AUTHOR: ---------YangGe (TOM) ,YangGe.freedom@icloud.com
# ORGANIZATION:
# CREATED: 2023-8-24 09:11:20
# REVISION: --------
#
#
#
#
#
#====================================================================================================================
# 定义一个检查进程号是否存在的函数
checkpid(){
# 定义一个本地变量i
local i
# 使用for循环遍历传递给该函数的所有的变量
for i in $*
do
# 如果目录/proc/$i存在,则执行此函数返回0
# 在一般的Linux系统中,如果进程正在运行,则在/proc目录下回存在一个进程号命令的子目录
[ -d "/proc/$i" ] && return 0
done# 返回1
return 1
}
# 调用函数checkpid
checkpid $pid1 $pid1399 $pid3# 如果上面的执行成功,即$?的值等于0,则执行if中的语句
if [ $? == 0 ]
then
echo "The one of them is running......"
else
echo "These PIDS are not running......"fi
4.2 测试检查PID
[root@www functiondic]# ./functionreturnvalue.sh
These PIDS are not running......
[root@www functiondic]# vi functionreturnvalue.sh
[root@www functiondic]# ./functionreturnvalue.sh
These PIDS are not running......