shell中函數(shù)的應(yīng)用
To turn the functions in this chapter into a library for use in other scripts, extract all the functions and concatenate them into one big file. If we call this file library.sh, a test script that accesses all of the functions might look like this:
#!/bin/sh
# Library test script
. library.sh
initializeANSI
echon "First off, do you have echo in your path? (1=yes, 2=no) "
read answer
while ! validint $answer 1 2 ; do
echon "${boldon}Try again${boldoff}. Do you have echo "
echon "in your path? (1=yes, 2=no) "
read answer
done
if ! checkForCmdInPath "echo" ; then
echo "Nope, can't find the echo command."
else
echo "The echo command is in the PATH."
fi
echo ""
echon "Enter a year you think might be a leap year: "
read year
while ! validint $year 1 9999 ; do
echon "Please enter a year in the ${boldon}correct${boldoff} format: "
read year
done
if isLeapYear $year ; then
echo "${greenf}You're right! $year was a leap year.${reset}"
else
echo "${redf}Nope, that's not a leap year.${reset}"
fi
exit 0
應(yīng)用函數(shù),我們就可以復(fù)用我們的腳本。
值得注意的是 $ . tinyscript.sh ,就是在當(dāng)前shell下執(zhí)行腳本,不加"."或source
則會(huì)在子shell下執(zhí)行腳本,可能會(huì)有不同的情況發(fā)生,值得注意。
- Shell中關(guān)于時(shí)間和日期的函數(shù)總結(jié)
- Linux 在Shell腳本中使用函數(shù)實(shí)例詳解
- shell 使用數(shù)組作為函數(shù)參數(shù)的方法(詳解)
- Shell使用Epoch進(jìn)行日期時(shí)間轉(zhuǎn)換和計(jì)算的幾個(gè)小函數(shù)
- Linux Shell函數(shù)返回值
- PowerShell中的函數(shù)重載示例
- Shell中函數(shù)返回值超出問(wèn)題
- Shell函數(shù)的7種用法介紹
- Shell腳本中使用function(函數(shù))示例
- 淺談Shell中的函數(shù)
相關(guān)文章
一文學(xué)會(huì)使用Linux內(nèi)核模塊&proc實(shí)例統(tǒng)計(jì)所有進(jìn)程信息
這篇文章主要介紹了使用Linux內(nèi)核模塊&proc實(shí)例統(tǒng)計(jì)所有進(jìn)程信息詳解,2023-05-05
shell腳本自動(dòng)修復(fù)mysql損壞的表
這篇文章主要介紹了shell腳本如何自動(dòng)修復(fù)mysql損壞的表,需要的朋友可以參考下2015-10-10
Shell腳本實(shí)戰(zhàn)之DNS主從同步腳本實(shí)例
這篇文章主要介紹了Shell腳本實(shí)戰(zhàn)之DNS主從同步腳本實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10
Linux實(shí)現(xiàn)定時(shí)備份MySQL數(shù)據(jù)庫(kù)并刪除30天前的備份文件
這篇文章主要介紹了Linux實(shí)現(xiàn)定時(shí)備份MySQL數(shù)據(jù)庫(kù)并刪除30天前的備份文件,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-01-01
shell編程中for循環(huán)語(yǔ)句的實(shí)現(xiàn)過(guò)程及案例
Bash?Shell中主要提供了三種循環(huán)方式:for、while和until,下面這篇文章主要給大家介紹了關(guān)于shell編程中for循環(huán)語(yǔ)句的實(shí)現(xiàn)過(guò)程及案例,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-04-04

