运维Shell脚本小试牛刀(七):在函数文脚本件中调用另外一个脚本文件中函数|函数递归调用|函数后台执行


运维Shell脚本小试牛刀(一)

运维Shell脚本小试牛刀(二)

运维Shell脚本小试牛刀(三)::$(cd $(dirname $0); pwd)命令详解

运维Shell脚本小试牛刀(四): 多层嵌套if...elif...elif....else fi_蜗牛杨哥的博客-CSDN博客

Cenos7安装小火车程序动画

运维Shell脚本小试牛刀(五):until循环

运维Shell脚本小试牛刀(六): Shell中的函数认知

运维Shell脚本小试牛刀(八): case模式忽略命令行参数大小写

运维Shell脚本小试牛刀(七):从函数文件中调用另外一个脚本文件中函数


简介: 从函数文件中调用函数

你可以把所有的函数存储在一个脚本文件

你可以把所有的函数加载到当前脚本文件或者时命令行

加载函数文件的所有函数的语法如下:

. /path/to/your/functions.sh


一: 编写函数文件

[root@www dicfor]# cat myfunctions.sh
#==================================================================================================================
#
#
#                           FILE:  myfunctions.sh
#                           USAGE: ./myfunctions.sh
#    DESCRIPTION:   函数定义:从函数文件中调用函数,可以把所有的函数存储在一个文件中,然后把所有的函数加载到当前脚本或是命令行
#        OPTIONS: -------
#        REQUIREMENTS: ---------

#        BUGS: ------
#        NOTES: -------- 
#        AUTHOR: ---------YangGe (TOM) ,YangGe.freedom@icloud.com
#    ORGANIZATION:
#        CREATED: 2023-8-24 09:11:20    
#        REVISION: --------
#
#
#
#
#
#====================================================================================================================
# 加载函数文件中的所有函数的语法如下: . /path/to/your/functions.sh

# 定义变量
declare -r TRUE=0
declare -r FALSE=0
declare -r PASSWD_FILE=/etc/passwd

###################################################################################################################
#
#   用途: 将字符串转换为小写
#     参数
#       $1 -> 要转换为小写的字符串
#
#
#
#
####################################################################################################################
function to_lower() {
  # 定义一个本地变量str
  local str="$@"
  # 定义本地变量output
  local output
  # 将变量str的值转换为小写符赋值给变量output
  output=$(tr ' [A-Z] ' ' [a-z] '<<<"${str}")
  echo $output

}

###################################################################################################################
#
#   用途: 如果脚本root用户执行则返回true
#     参数 无
#   返回值: True或者Flase
#
#
#
#
####################################################################################################################

function is_root()
{
  # 如果运行此脚本的账号的uid等于0,则返回0,否则返回1
  [ $(id -u) -eq 0 ] && return $TRUE || return $FALSE


}
###################################################################################################################
#
#   用途: 如果用户名存在于文件/etc/passwd 中则返回true
#     参数
#       $1 (用户名) -> 要在文件/etc/passwd 中检查的用户名
#       返回值: True 或者 False
#
#
#
#
####################################################################################################################
function is_user_exits()
{
   # 定义本地变量u
   local u="$1"
   # 如果文件/etc/passwd中存在以变量$u的值为开头的行,则返回0,否则返回1
   grep -q "^${u}" $PASSWD_FILE && return $TRUE || return $FALSE

}
 

二: 加载函数文件到当前shell环境


[root@www dicfor]# pwd
/usr/local/example/dicfor
[root@www dicfor]# . /usr/local/example/dicfor/myfunctions.sh
-bash: declare: TRUE: 只读变量
-bash: declare: FALSE: 只读变量
-bash: declare: PASSWD_FILE: 只读变量
 

三: 编写加载myfunctions.sh函数文件的脚本文件 


[root@www dicfor]# cat myfunctionsdemo.sh 
#==================================================================================================================
#
#
#                           FILE:  functionsdemo.sh
#                           USAGE: ./functionsdemo.sh
#    DESCRIPTION:   函数定义,在该文件中加载一个函数文件myfunctions.sh到该脚本文件中
#        OPTIONS: -------
#        REQUIREMENTS: ---------

#        BUGS: ------
#        NOTES: -------- 
#        AUTHOR: ---------YangGe (TOM) ,YangGe.freedom@icloud.com
#    ORGANIZATION:
#        CREATED: 2023-8-24 09:11:20    
#        REVISION: --------
#
#
#
#
#
#====================================================================================================================
# 加载函数文件myfunctions.sh
# 这里的路径需要根据你的实际环境作出跳转
. /usr/local/example/dicfor/myfunctions.sh

# 定义本地变量
# var1时没有被myfunctions.sh使用的
var1="The Mahabharata is the longest and,arguably,one of the greatest epicpoems is any language.."

# 调用函数is root , 指定成功或失败,会分别打印不同的信息
is_root && echo "You are logged in as root." || echo "You are not logged in as root"

# 调用函数is_use_exits
is_user_exits "mysql" && echo "Account found." || echo "Account not found."

# 打印变量的值var1
echo -e "*** Orignal quote: \n${var1}" 

# 调用函数to_lower()
# 将#var1 作为参数传递给to_lower()

# 在echo 内使用的命令替换
echo -e "*** Lowercase version: \n$(to_lower ${var1})"
 

四: 执行该脚本|看看该脚本是否已调用引入的脚本函数


[root@www dicfor]# ./myfunctionsdemo.sh 
You are logged in as root.
Account found.
*** Orignal quote: 
The Mahabharata is the longest and,arguably,one of the greatest epicpoems is any language..
*** Lowercase version: 
the mahabharata is the longest and,arguably,one of the greatest epicpoems is any language..

五: 函数递归调用 

[root@www functiondic]# cat functionnestedCalled.sh 
#==================================================================================================================
#
#
#                           FILE:  functionNestedCalled.sh
#                           USAGE: ./functionNestedCalled.sh
#    DESCRIPTION:  Shell中函数递归调用
#        OPTIONS: -------
#        REQUIREMENTS: ---------

#        BUGS: ------
#        NOTES: -------- 
#        AUTHOR: ---------YangGe (TOM) ,YangGe.freedom@icloud.com
#    ORGANIZATION:
#        CREATED: 2023-8-24 09:11:20    
#        REVISION: --------
#
#
#
#
#
#====================================================================================================================
# 定义函数factorial()--计算给定命令行参数的阶层
function factorial {

  # 定义本地变量i
  local i=$1
  # 定义本地变量f
  local f
  # 声明变量为整数
  declare -i i
  # 声明变量f为整数
  declare -i f

  # factorial 函数被调用,只到调用$f的值<-2
  # 开始递归
 [ $1 -le 2 ] && echo $i || { f=$(( i - 1)); f=$(factorial $f); f=$(( f * i )); echo ${f}; } 

}

# 显示函数用法
[ $# -eq 0 ] &&  { echo "Usage: $0 number"; exit 1; }

# 调用函数factorial
factorial $1
 


执行脚本:

[root@www functiondic]# ./functionnestedCalled.sh 
Usage: ./functionnestedCalled.sh number
[root@www functiondic]# ./functionnestedCalled.sh 2
2
[root@www functiondic]# ./functionnestedCalled.sh 4
24
[root@www functiondic]# ./functionnestedCalled.sh 24
-7835185981329244160
[root@www functiondic]# ./functionnestedCalled.sh 10
3628800
 

六:  Shell脚本函数后台执行


[root@www functiondic]# cat saemoncalledFunction.sh 
#==================================================================================================================
#
#
#                           FILE:  saemoncalledFunction.sh
#                           USAGE: ./saemoncalledFunction.sh
#    DESCRIPTION:  Shell 中函数放在后台执行
#        OPTIONS: -------
#        REQUIREMENTS: ---------

#        BUGS: ------
#        NOTES: -------- 
#        AUTHOR: ---------YangGe (TOM) ,YangGe.freedom@icloud.com
#    ORGANIZATION:
#        CREATED: 2023-8-24 09:11:20    
#        REVISION: --------
#
#
#
#
#
#====================================================================================================================
# 定义函数progress,显示进度条
progress(){
 echo -n "$0: Please wait.............."
  # 执行无限while循环
  while true
  do
    echo -n "."
    # 休眠5秒
    sleep 5
  done

}

# 定义函数dobackup
dobackup(){
 # 运行备份命令
 tar -zcvf /dev/st0 /home >/dev/null 2>&1

}

# 将函数放在后台运行
progress &

# 保存函数progress()运行的进程号
# 需要使用PID来结束此函数
MYSELF=$!

# 开始备份
# 转移控制到函数dobackup
dobackup

# 杀死进程
kill $MYSELF > /dev/null 2>&1
echo -n ".....done."
echo
 


脚本执行效果:


[root@www functiondic]# ./saemoncalledFunction.sh 
./saemoncalledFunction.sh: Please wait.....................done.
 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值