您现在的位置是:网站首页> 编程资料编程资料
shell中函数的应用_linux shell_
2023-05-26
390人已围观
简介 shell中函数的应用_linux 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下执行脚本,可能会有不同的情况发生,值得注意。
您可能感兴趣的文章:
相关内容
- 101个shell脚本 猜数字游戏代码_linux shell_
- Formatting Long Lines 格式化多行字符的shell脚本_linux shell_
- Git 常用命令整理_linux shell_
- git多账号登录问题解析_linux shell_
- 一个简单的linux命令 mv_linux shell_
- 一个简单的linux命令 cp_linux shell_
- 一个简单的linux命令 touch_linux shell_
- linux定时任务基础命令介绍(14)_linux shell_
- linux文件搜索及其它基础命令介绍(3)_linux shell_
- linux文本过滤grep基础命令介绍(5)_linux shell_
