運維Shell腳本小試牛刀(一)
運維Shell腳本小試牛刀(二)
運維Shell腳本小試牛刀(三)::$(cd $(dirname $0); pwd)命令詳解
運維Shell腳本小試牛刀(四): 多層嵌套if...elif...elif....else fi_蝸牛楊哥的博客-CSDN博客
Cenos7安裝小火車程序動畫
運維Shell腳本小試牛刀(五):until循環(huán)
運維Shell腳本小試牛刀(六): Shell中的函數(shù)認知
運維Shell腳本小試牛刀(八): case模式忽略命令行參數(shù)大小寫
運維Shell腳本小試牛刀(七):從函數(shù)文件中調(diào)用另外一個腳本文件中函數(shù)
簡介: 從函數(shù)文件中調(diào)用函數(shù)
你可以把所有的函數(shù)存儲在一個腳本文件
你可以把所有的函數(shù)加載到當前腳本文件或者時命令行
加載函數(shù)文件的所有函數(shù)的語法如下:
. /path/to/your/functions.sh
一: 編寫函數(shù)文件
[root@www dicfor]# cat myfunctions.sh
#==================================================================================================================
#
#
# ? ? ? ? ? ? ? ? ? ? ? ? ? FILE: ?myfunctions.sh
# ? ? ? ? ? ? ? ? ? ? ? ? ? USAGE: ./myfunctions.sh
# ? ?DESCRIPTION: ? 函數(shù)定義:從函數(shù)文件中調(diào)用函數(shù),可以把所有的函數(shù)存儲在一個文件中,然后把所有的函數(shù)加載到當前腳本或是命令行
# ? ? ? ?OPTIONS: -------
# ? ? ? ?REQUIREMENTS: ---------
#?
# ? ? ? ?BUGS: ------
# ? ? ? ?NOTES: --------?
# ? ? ? ?AUTHOR: ---------YangGe (TOM) ,YangGe.freedom@icloud.com
# ? ?ORGANIZATION:
# ? ? ? ?CREATED: 2023-8-24 09:11:20 ? ?
# ? ? ? ?REVISION: --------
#
#
#
#
#
#====================================================================================================================
# 加載函數(shù)文件中的所有函數(shù)的語法如下: . /path/to/your/functions.sh# 定義變量
declare -r TRUE=0
declare -r FALSE=0
declare -r PASSWD_FILE=/etc/passwd###################################################################################################################
#
# ? 用途: 將字符串轉(zhuǎn)換為小寫
# ? ? 參數(shù)
# ? ? ? $1 -> 要轉(zhuǎn)換為小寫的字符串
#
#
#
#
####################################################################################################################
function to_lower() {
? # 定義一個本地變量str
? local str="$@"
? # 定義本地變量output
? local output
? # 將變量str的值轉(zhuǎn)換為小寫符賦值給變量output
? output=$(tr ' [A-Z] ' ' [a-z] '<<<"${str}")
? echo $output}
###################################################################################################################
#
# ? 用途: 如果腳本root用戶執(zhí)行則返回true
# ? ? 參數(shù) 無
# ? 返回值: True或者Flase
#
#
#
#
####################################################################################################################function is_root()
{
? # 如果運行此腳本的賬號的uid等于0,則返回0,否則返回1
? [ $(id -u) -eq 0 ] && return $TRUE || return $FALSE
}
###################################################################################################################
#
# ? 用途: 如果用戶名存在于文件/etc/passwd 中則返回true
# ? ? 參數(shù)
# ? ? ? $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}
?
二: 加載函數(shù)文件到當前shell環(huán)境
[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函數(shù)文件的腳本文件?
[root@www dicfor]# cat myfunctionsdemo.sh?
#==================================================================================================================
#
#
# ? ? ? ? ? ? ? ? ? ? ? ? ? FILE: ?functionsdemo.sh
# ? ? ? ? ? ? ? ? ? ? ? ? ? USAGE: ./functionsdemo.sh
# ? ?DESCRIPTION: ? 函數(shù)定義,在該文件中加載一個函數(shù)文件myfunctions.sh到該腳本文件中
# ? ? ? ?OPTIONS: -------
# ? ? ? ?REQUIREMENTS: ---------
#?
# ? ? ? ?BUGS: ------
# ? ? ? ?NOTES: --------?
# ? ? ? ?AUTHOR: ---------YangGe (TOM) ,YangGe.freedom@icloud.com
# ? ?ORGANIZATION:
# ? ? ? ?CREATED: 2023-8-24 09:11:20 ? ?
# ? ? ? ?REVISION: --------
#
#
#
#
#
#====================================================================================================================
# 加載函數(shù)文件myfunctions.sh
# 這里的路徑需要根據(jù)你的實際環(huán)境作出跳轉(zhuǎn)
. /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.."# 調(diào)用函數(shù)is root , 指定成功或失敗,會分別打印不同的信息
is_root && echo "You are logged in as root." || echo "You are not logged in as root"# 調(diào)用函數(shù)is_use_exits
is_user_exits "mysql" && echo "Account found." || echo "Account not found."# 打印變量的值var1
echo -e "*** Orignal quote: \n${var1}"?# 調(diào)用函數(shù)to_lower()
# 將#var1 作為參數(shù)傳遞給to_lower()# 在echo 內(nèi)使用的命令替換
echo -e "*** Lowercase version: \n$(to_lower ${var1})"
?
四: 執(zhí)行該腳本|看看該腳本是否已調(diào)用引入的腳本函數(shù)
[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..
五: 函數(shù)遞歸調(diào)用?
[root@www functiondic]# cat functionnestedCalled.sh?
#==================================================================================================================
#
#
# ? ? ? ? ? ? ? ? ? ? ? ? ? FILE: ?functionNestedCalled.sh
# ? ? ? ? ? ? ? ? ? ? ? ? ? USAGE: ./functionNestedCalled.sh
# ? ?DESCRIPTION: ?Shell中函數(shù)遞歸調(diào)用
# ? ? ? ?OPTIONS: -------
# ? ? ? ?REQUIREMENTS: ---------
#?
# ? ? ? ?BUGS: ------
# ? ? ? ?NOTES: --------?
# ? ? ? ?AUTHOR: ---------YangGe (TOM) ,YangGe.freedom@icloud.com
# ? ?ORGANIZATION:
# ? ? ? ?CREATED: 2023-8-24 09:11:20 ? ?
# ? ? ? ?REVISION: --------
#
#
#
#
#
#====================================================================================================================
# 定義函數(shù)factorial()--計算給定命令行參數(shù)的階層
function factorial {? # 定義本地變量i
? local i=$1
? # 定義本地變量f
? local f
? # 聲明變量為整數(shù)
? declare -i i
? # 聲明變量f為整數(shù)
? declare -i f? # factorial 函數(shù)被調(diào)用,只到調(diào)用$f的值<-2
? # 開始遞歸
?[ $1 -le 2 ] && echo $i || { f=$(( i - 1)); f=$(factorial $f); f=$(( f * i )); echo ${f}; }?}
# 顯示函數(shù)用法
[ $# -eq 0 ] && ?{ echo "Usage: $0 number"; exit 1; }# 調(diào)用函數(shù)factorial
factorial $1
?
執(zhí)行腳本:
[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腳本函數(shù)后臺執(zhí)行
[root@www functiondic]# cat saemoncalledFunction.sh?
#==================================================================================================================
#
#
# ? ? ? ? ? ? ? ? ? ? ? ? ? FILE: ?saemoncalledFunction.sh
# ? ? ? ? ? ? ? ? ? ? ? ? ? USAGE: ./saemoncalledFunction.sh
# ? ?DESCRIPTION: ?Shell 中函數(shù)放在后臺執(zhí)行
# ? ? ? ?OPTIONS: -------
# ? ? ? ?REQUIREMENTS: ---------
#?
# ? ? ? ?BUGS: ------
# ? ? ? ?NOTES: --------?
# ? ? ? ?AUTHOR: ---------YangGe (TOM) ,YangGe.freedom@icloud.com
# ? ?ORGANIZATION:
# ? ? ? ?CREATED: 2023-8-24 09:11:20 ? ?
# ? ? ? ?REVISION: --------
#
#
#
#
#
#====================================================================================================================
# 定義函數(shù)progress,顯示進度條
progress(){
?echo -n "$0: Please wait.............."
? # 執(zhí)行無限while循環(huán)
? while true
? do
? ? echo -n "."
? ? # 休眠5秒
? ? sleep 5
? done}
# 定義函數(shù)dobackup
dobackup(){
?# 運行備份命令
?tar -zcvf /dev/st0 /home >/dev/null 2>&1}
# 將函數(shù)放在后臺運行
progress &# 保存函數(shù)progress()運行的進程號
# 需要使用PID來結(jié)束此函數(shù)
MYSELF=$!# 開始備份
# 轉(zhuǎn)移控制到函數(shù)dobackup
dobackup# 殺死進程
kill $MYSELF > /dev/null 2>&1
echo -n ".....done."
echo
?
腳本執(zhí)行效果:文章來源:http://www.zghlxwxcb.cn/news/detail-701851.html
[root@www functiondic]# ./saemoncalledFunction.sh?
./saemoncalledFunction.sh: Please wait.....................done.
?文章來源地址http://www.zghlxwxcb.cn/news/detail-701851.html
到了這里,關(guān)于運維Shell腳本小試牛刀(七):在函數(shù)文腳本件中調(diào)用另外一個腳本文件中函數(shù)|函數(shù)遞歸調(diào)用|函數(shù)后臺執(zhí)行的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!