主页 > 知识库 > shell中函数的应用

shell中函数的应用

热门标签:百度竞价排名 地方门户网站 铁路电话系统 服务外包 呼叫中心市场需求 网站排名优化 Linux服务器 AI电销

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

您可能感兴趣的文章:
  • Shell中关于时间和日期的函数总结
  • Linux 在Shell脚本中使用函数实例详解
  • shell 使用数组作为函数参数的方法(详解)
  • Shell使用Epoch进行日期时间转换和计算的几个小函数
  • Linux Shell函数返回值
  • PowerShell中的函数重载示例
  • Shell中函数返回值超出问题
  • Shell函数的7种用法介绍
  • Shell脚本中使用function(函数)示例
  • 浅谈Shell中的函数

标签:黄山 崇左 湘潭 兰州 湖南 仙桃 衡水 铜川

巨人网络通讯声明:本文标题《shell中函数的应用》,本文关键词  ;如发现本文内容存在版权问题,烦请提供相关信息告之我们,我们将及时沟通与处理。本站内容系统采集于网络,涉及言论、版权与本站无关。
  • 相关文章
  • 收缩
    • 微信客服
    • 微信二维码
    • 电话咨询

    • 400-1100-266