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下执行脚本,可能会有不同的情况发生,值得注意。
热门推荐
3 祝福语简短的微信
10 幼儿祝福语大全简短贺卡
11 感谢教官文案祝福语简短
12 鼠年同事祝福语简短创意
13 元旦祝福语 简短独特群发
14 妈咪跟我生日祝福语简短
15 给儿子新婚祝福语简短
16 伴娘祝福语长文案简短
17 恭喜高考完祝福语简短
18 生日祝福语送老公简短