shell中函数的应用
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下执行脚本,可能会有不同的情况发生,值得注意。
相关推荐
firefaith 2020-10-30
以梦为马不负韶华 2020-10-20
wangzhaotongalex 2020-09-22
jarrygao 2020-11-02
woaimeinuo 2020-10-21
laisean 2020-09-01
libao 2020-09-16
卖口粥湛蓝的天空 2020-09-15
laisean 2020-11-11
Julyth 2020-10-16
laisean 2020-09-27
flycappuccino 2020-09-27
liguojia 2020-09-27
huha 2020-10-16
大牛牛 2020-10-30
liguojia 2020-10-20
wangzhaotongalex 2020-10-20
JohnYork 2020-10-16
applecarelte 2020-10-16
流年浅滩 2020-10-23
liujianhua 2020-10-22
tufeiax 2020-09-03
vvu 2020-09-16
Yyqingmofeige 2020-08-18
zhushixia 2020-08-17