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下执行脚本,可能会有不同的情况发生,值得注意。
热门推荐
1 训练狗狗憋尿
2 狗狗追踪训练
3 狗狗训练要多少钱
5 深圳狗狗训练
6 训练狗狗唤回
7 狗狗叫怎么训练
8 狗狗训练班
10 狗狗社会化训练
11 狗狗叫回训练
12 广州狗狗训练学校
13 狗狗训练躺下
14 狗狗随行训练
15 训练狗狗巡回
16 狗狗要怎么训练
17 狗狗喂食训练
18 训练狗狗躺下