shell中函数的应用

所属分类: 脚本专栏 / linux shell 阅读数: 518
收藏 0 赞 0 分享

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下执行脚本,可能会有不同的情况发生,值得注意。

更多精彩内容其他人还在看

linux whatis与whatis database 使用及查询方法(man使用实例)

在学习man命令时候,估计很多朋友都发现有man –f ,man –k 参数,可以查出很多数据。这些有的与man手册页相同,有些不是属于手册页的。它们数据怎么收集来的,并且whatis是怎么样工作的
收藏 0 赞 0 分享

linux dev 常见特殊设备介绍与应用(loop,null,zero,full,random)

这篇文章主要介绍了linux dev 常见特殊设备介绍与应用(loop,null,zero,full,random),需要的朋友可以参考下
收藏 0 赞 0 分享

linux shell命令快捷获得系统帮助(一)[man-pages定义规范]

linux命令帮助,一般有2种,命令自身代码里面带有使用帮助说明,这种一般很精简,太长了,程序自身的大小以及日常维护不方便。还有一种,就是带有帮助文件,类似windows的chm格式文件。下面我说下这2种怎么样查阅
收藏 0 赞 0 分享

linux shell实现随机数几种方法分享(date,random,uuid)

这篇文章主要介绍了linux shell实现随机数多种方法(date,random,uuid),需要的朋友可以参考下
收藏 0 赞 0 分享

linux shell 脚本实现tcp/upd协议通讯(重定向应用)

这篇文章主要介绍了linux shell 脚本实现tcp/upd协议通讯(重定向应用),需要的朋友可以参考下
收藏 0 赞 0 分享

linux shell数据重定向(输入重定向与输出重定向)详细分析

这篇文章主要介绍了linux shell数据重定向(输入重定向与输出重定向)详细分析,需要的朋友可以参考下
收藏 0 赞 0 分享

linux shell 管道命令(pipe)使用及与shell重定向区别

这篇文章主要介绍了linux shell 管道命令(pipe)使用及与shell重定向区别,需要的朋友可以参考下
收藏 0 赞 0 分享

Linux下使用tcpdump抓包的实现方法

tcpdump是Linux下面的一个开源的抓包工具,和Windows下面的wireshark抓包工具一样, 支持抓取指定网口、指定目的地址、指定源地址、指定端口、指定协议的数据。这篇文章主要介绍了Linux下使用tcpdump抓包的实现方法,需要的朋友可以参考下
收藏 0 赞 0 分享

零基础入门篇之Linux及Arm-Linux程序开发笔记

这篇文章主要介绍了零基础入门篇之Linux及Arm-Linux程序开发笔记,需要的朋友可以参考下
收藏 0 赞 0 分享

Linux 中 CURL常用命令详解

这篇文章主要介绍了Linux 中 CURL常用命令详解,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多