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)文章
shell自定義函數(shù)的6個(gè)特點(diǎn)總結(jié)
這篇文章主要介紹了shell自定義函數(shù)的6個(gè)特點(diǎn)總結(jié),也是使用shell自定義函數(shù)的一些注意事項(xiàng)總結(jié),以及小技巧介紹,需要的朋友可以參考下2014-07-07PXE kickstart自動(dòng)化部署系統(tǒng)安裝
這篇文章主要為大家介紹了PXE kickstart自動(dòng)化部署系統(tǒng)的過(guò)程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-03-03Shell腳本實(shí)現(xiàn)隨機(jī)數(shù)多種方法介紹(date、random、uuid)
這篇文章主要介紹了Shell腳本實(shí)現(xiàn)隨機(jī)數(shù)多種方法介紹,本文講解了通過(guò)時(shí)間獲得隨機(jī)數(shù)、通過(guò)內(nèi)部系統(tǒng)變量、通過(guò)系統(tǒng)內(nèi)部唯一數(shù)據(jù)生成隨機(jī)數(shù)等方法,需要的朋友可以參考下2014-11-11linux 檢測(cè)遠(yuǎn)程端口是否打開(kāi)方法總結(jié)
這篇文章主要介紹了linux 檢測(cè)遠(yuǎn)程端口是否打開(kāi)方法總結(jié)的相關(guān)資料,需要的朋友可以參考下2017-04-04獲取服務(wù)器信息的Shell腳本分享(ubuntu、centos測(cè)試通過(guò))
這篇文章主要介紹了獲取服務(wù)器信息的Shell腳本分享(ubuntu、centos測(cè)試通過(guò)),本文直接給出實(shí)現(xiàn)代碼,本文腳本實(shí)現(xiàn)獲取linux發(fā)行版名稱、查看系統(tǒng)是否為64位、系統(tǒng)內(nèi)核版本等信息,需要的朋友可以參考下2014-12-12快速入門(mén)Shell腳本之條件判斷語(yǔ)句與循環(huán)
這篇文章主要介紹了快速入門(mén)Shell腳本之條件判斷語(yǔ)句與循環(huán),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12linux shell之通過(guò)標(biāo)識(shí)測(cè)試文件系統(tǒng)屬性的方法示例
今天小編就為大家分享一篇關(guān)于linux shell之通過(guò)標(biāo)識(shí)測(cè)試文件系統(tǒng)屬性的方法示例,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-04-04