shell函数 shell中函数的应用

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

shell函数 shell中函数的应用

  2021-03-22 我要评论
想了解shell中函数的应用的相关内容吗,在本文为您仔细讲解shell函数的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:shell函数,下面大家一起来学习吧。

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

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们