shell中函数的应用
Toturnthefunctionsinthischapterintoalibraryforuseinotherscripts,extractallthefunctionsandconcatenatethemintoonebigfile.Ifwecallthisfilelibrary.sh,atestscriptthataccessesallofthefunctionsmightlooklikethis:
#!/bin/sh
#Librarytestscript
.library.sh
initializeANSI
echon"Firstoff,doyouhaveechoinyourpath?(1=yes,2=no)"
readanswer
while!validint$answer12;do
echon"${boldon}Tryagain${boldoff}.Doyouhaveecho"
echon"inyourpath?(1=yes,2=no)"
readanswer
done
if!checkForCmdInPath"echo";then
echo"Nope,can'tfindtheechocommand."
else
echo"TheechocommandisinthePATH."
fi
echo""
echon"Enterayearyouthinkmightbealeapyear:"
readyear
while!validint$year19999;do
echon"Pleaseenterayearinthe${boldon}correct${boldoff}format:"
readyear
done
ifisLeapYear$year;then
echo"${greenf}You'reright!$yearwasaleapyear.${reset}"
else
echo"${redf}Nope,that'snotaleapyear.${reset}"
fi
exit0
应用函数,我们就可以复用我们的脚本。
值得注意的是$.tinyscript.sh,就是在当前shell下执行脚本,不加"."或source
则会在子shell下执行脚本,可能会有不同的情况发生,值得注意。
热门推荐
7 简短的二胎祝福语
10 简短霸气女儿生日 祝福语
11 祝福语高考小众诗句简短
12 元旦祝福语20秒简短
13 老师过年祝福语大全 简短
14 一生祝福语简短
15 产检祝福语简短霸气
16 朋友店开张祝福语简短
17 生日爱情祝福语大全简短
18 朋友女儿生日祝福语 简短