手机
当前位置:查字典教程网 >脚本专栏 >linuxshell >shell中函数的应用
shell中函数的应用
摘要:Toturnthefunctionsinthischapterintoalibraryforuseinotherscripts,extrac...

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" read answer while ! validint $answer 1 2 ; do echon "${boldon}Try again${boldoff}. Do you have echo " echon "in your path" 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下执行脚本,可能会有不同的情况发生,值得注意。

【shell中函数的应用】相关文章:

Shell脚本传递参数的3种方法比较

shell脚本中常见的一些特殊符号和作用详解

Linux bash Shell中的变量类型详解

shell自定义函数及参数调用解析

shell中的各种括号的使用方法

Shell脚本中使用function(函数)示例

shell脚本中28个特殊字符的作用简明总结

一个shell写的ping函数

Shell中的${}、##和%%使用范例

shell的比较运算介绍

精品推荐
分类导航