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
應用函數,我們就可以復用我們的腳本。
值得注意的是 $ . tinyscript.sh ,就是在當前shell下執行腳本,不加"."或source
則會在子shell下執行腳本,可能會有不同的情況發生,值得注意。
您可能感興趣的文章:- Shell中關于時間和日期的函數總結
- Linux 在Shell腳本中使用函數實例詳解
- shell 使用數組作為函數參數的方法(詳解)
- Shell使用Epoch進行日期時間轉換和計算的幾個小函數
- Linux Shell函數返回值
- PowerShell中的函數重載示例
- Shell中函數返回值超出問題
- Shell函數的7種用法介紹
- Shell腳本中使用function(函數)示例
- 淺談Shell中的函數