读书笔记--101个shell脚本 之#12--函数
2011-06-27 20:53
441 查看
今天这个脚本其实很简单,但很实用,讲的是shell中函数的应用
应用函数,我们就可以复用我们的脚本。
值得注意的是 $ . tinyscript.sh ,就是在当前shell下执行脚本,不加"."或source[/b]
则会在子shell下执行脚本,可能会有不同的情况发生,值得注意。[/b]
The Code
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[/b]
则会在子shell下执行脚本,可能会有不同的情况发生,值得注意。[/b]
相关文章推荐
- 读书笔记--101个shell脚本 之#11--炫
- 读书笔记--101个shell脚本--05
- 《Linux命令、编辑器与Shell编程》读书笔记12-函数和脚本
- 读书笔记--101个shell脚本 之#9
- 读书笔记--101个shell脚本 之#13--猜数字
- 读书笔记--101个shell脚本 之#10
- shell脚本学习--函数篇
- 【Shell 编程基础第二部分】Shell里的流程控制、Shell里的函数及脚本调试方法
- Shell脚本(函数,shell中的数组)
- shell脚本里常见的指令和函数
- Linux新手生存笔记[10]——shell脚本基础3-函数及常用命令
- 12_Shell语言―――脚本的书写规范以及检查和调试脚本
- 【Linux】中的shell脚本语法篇之函数和数组
- Shell中脚本变量和函数变量的作用域
- 【Shell 编程基础第二部分】Shell里的流程控制、Shell里的函数及脚本调试方法!
- linux下shell脚本的使用——安装madplay播放器,sed函数详解
- 【Shell 编程基础第二部分】Shell里的流程控制、Shell里的函数及脚本调试方法!
- shell脚本功能函数集合
- shell脚本帮助函数
- 【Shell脚本学习22】Shell 函数:Shell函数返回值、删除函数、在终端调用函数