读书笔记--101个shell脚本 之#12--函数
今天这个脚本其实很简单,但很实用,讲的是shell中函数的应用 The CodeTo 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下执行脚本,可能会有不同的情况发生,值得注意. 本文出自 “你就当我是浮夸吧” 博客,请务必保留此出处http://2804976.blog.51cto.com/2794976/597476 |
凌众科技专业提供服务器租用、服务器托管、企业邮局、虚拟主机等服务,公司网站:http://www.lingzhong.cn 为了给广大客户了解更多的技术信息,本技术文章收集来源于网络,凌众科技尊重文章作者的版权,如果有涉及你的版权有必要删除你的文章,请和我们联系。以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢! |