博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
读书笔记--101个shell脚本 之#12--函数
阅读量:7186 次
发布时间:2019-06-29

本文共 1219 字,大约阅读时间需要 4 分钟。

今天这个脚本其实很简单,但很实用,讲的是shell中函数的应用

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.shinitializeANSIechon "First off, do you have echo in your path? (1=yes, 2=no) "read answerwhile ! validint $answer 1 2 ; do  echon "${boldon}Try again${boldoff}. Do you have echo "  echon "in your path? (1=yes, 2=no) "  read answerdoneif ! checkForCmdInPath "echo" ; then  echo "Nope, can't find the echo command."else  echo "The echo command is in the PATH."fiecho ""echon "Enter a year you think might be a leap year: "read yearwhile ! validint $year 1 9999 ; do  echon "Please enter a year in the ${boldon}correct${boldoff} format: "  read yeardoneif 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}"fiexit 0

应用函数,我们就可以复用我们的脚本。

值得注意的是 $ . tinyscript.sh  ,就是在当前shell下执行脚本,不加"."或source

则会在子shell下执行脚本,可能会有不同的情况发生,值得注意。

      本文转自hb_fukua  51CTO博客,原文链接:http://blog.51cto.com/2804976/597476,如需转载请自行联系原作者

你可能感兴趣的文章
Eclipse中从svn上检出项目无法识别jar包
查看>>
shell实现FTP自动批量上传下载文件
查看>>
我的友情链接
查看>>
Mybatis使用存储过程以及原理
查看>>
通讯录二维码使英文变为中文
查看>>
快2012了!
查看>>
【Objective-C】OC中字典基本概念和常用方法(NSDictionary和NSMutableDictionary)
查看>>
maven打包包含配置文件
查看>>
C# readonly与const比较
查看>>
我的友情链接
查看>>
我的大学n糊口
查看>>
设置easyUi弹出窗在弹出框(iframe)中不居中问题
查看>>
&.nbsp;在IE和谷歌浏览器中不兼容的解决办法
查看>>
Servlet容器
查看>>
捷为科技签约镇海石化, 打造石化建安行业生产项目信息化管理系统
查看>>
我的友情链接
查看>>
我的友情链接
查看>>
Java异常
查看>>
Java开发中的23种设计模式详解
查看>>
Spring Aop实例
查看>>