6.shell函数
    6.1.定义函数
        简介:
            shell允许将一组命令集或语句形成一个可用块,这些块成为shell函数
        定义函数的格式
            方法一
                函数名()
                {
                    命令1
                    ......
                }
            方法二
                function 函数名()
                {
                    命令1
                    ......
                }
            函数定义的两种方式
                函数可以放在同一个文件中作为一段代码,也可以放在只包含函数的单独文件中
                例子,在一个单独文件中只定义以下函数:

#!/bin/bash
#hellofun
function hello()
{
echo "Hello, today is `date`"
#`date` date用反引号括起来,表示在这条语句中先执行date命令,并将date命令的结果作为替换date位置的字符串
return 1
}

6.2.函数调用
        例子

#!/bin/bash
#func
function hello()
{
echo "Hello,today is `date`"
}
echo "now going to the fuction hello"
#接下来就是调用函数hello
hello
echo "back from the function hello"

6.3.参数传递
        简介
            向函数传递参数就像在脚本中使用位置变量 $1、$2、...$9
        例子

#!/bin/bash
#func
function hello()
{
echo "Hello, $1 today is `date`"
}
echo "now going to the fuction hello"
#接下来就是调用函数hello,传入参数 china
hello china
echo "back from the function hello"

6.4.函数文件
        简介:
            可以将函数定义和函数调用放在同一个文件中,也可以在一个文件中专门定义函数,在其他文件来调用该函数
        例子:
            在文件 hellofun 中定义函数

#!/bin/bash
function hello()
{
echo "Hello, today is `date`"
return 1
}

在另一个文件 func 中调用该函数

#!/bin/bash
#载入文件,标明定义函数的文件,格式为:.空格文件名(特别注意一定要在.和文件名之间有空格)
. hellofun
echo "now going to the fuction hello"
hello
echo "back from the function hello"

建议:
            学习shell脚本的时候,可以多看看linux系统的启动文件来学习,但是注意在看的过程中,如果想要对它进行修改,一定要首先备份一下这个文件,在对其进行修改,以便能在出错时使用备份文件挽回
    6.5.载入和删除函数
        检查载入函数和删除函数
            查看载入函数
                set命令查看这个函数如何载入
                例子:

#!/bin/bash
. hellofun
set
echo "now going to the fuction hello"
hello
echo "back from the function hello"

删除函数
                unset命令
                例子

#!/bin/bash
. hellofun
set
#使用unset使得接下来执行hello时候不能执行成功,因为unset删除了函数
unset hello
echo "now going to the fuction hello"
hello
#这里会输出信息:./func:hello:command not found
echo "back from the function hello"

6.6.函数返回状态值
        例子1:

#!/bin/bash
function hello()
{
echo "Hello, today is `date`"
return 0
}
echo "now going to the function hello"
hello
#接下来来输出返回状态值,用 $? 表示
echo $?
echo "back from the function hello"

例子2:

#!/bin/bash
function hello()
{
echo "Hello, today is `date`"
return 0
}
echo "now going to the function hello"
returnvalue=hello
#接下来来输出返回状态值,用 $? 表示
echo $?
echo $returnvalue
echo "back from the function hello"
#注意:不同于C语言等语言,shell脚本中不能将函数的返回值赋给变量
#returnvalue=hello 不是将hello函数的返回值赋给returnvalue,而是将hello字符串赋给returnvalue

Shell编程基础教程6--shell函数的更多相关文章

  1. 【shell编程基础1】shell变量篇

    Bash shell bash shell 是bourne shell 的升级版,“bourne again shell”.ubuntu的默认shell. 预备知识 1. "#!" ...

  2. Shell编程基础教程2--变量和运算符

    2.变量和运算符 2.1.变量的类型 本地变量:环境变量:变量替换(显示变量):位置变量:标准变量:特殊变量: 2.2.本地变量 本地变量在用户现在的shell生命周期的脚本中使用 在命令行, LOC ...

  3. Shell编程基础教程5--文本过滤、正则表达式、相关命令

    5.文本过滤.正则表达式.相关命令    5.1.正则表达式(什么是正则表达式?正则表达式怎么进行匹配?常用命令)        简介:            一种用来描述文本模式的特殊语法      ...

  4. Shell编程基础教程4--控制流结构

    4.控制流结构    4.1.控制结构            4.2.if then else语句        格式: if 条件1 //如果条件1为真 then 命令1 //那么,执行命令1 el ...

  5. Shell编程基础教程3--Shell输入与输出

    3.Shell输入与输出    3.1.echo        echo命令可以显示文本行或变量,或者把字符串输出到文件        echo [option] string             ...

  6. Shell编程基础教程1--Shell简介

    1.Shell简介 1.1.查看你系统shell信息 cat /etc/shell 命令可以获取Linux系统里面有多少种shell程序 echo $SHELL 命令可以查看当前你所使用的shell是 ...

  7. Shell编程基础教程7--脚本参数的传递

    7.脚本参数的传递    7.1.shift命令        简介:            shift n        每次将参数位置向左偏移n位        例子 #!/bin/bash us ...

  8. 【shell编程基础2】shell组合应用之一:重定向和逻辑

    这篇主要讲下 数据的重定向,在shell脚本中有些重要的输出重定向为文件的形式输出来 逻辑方式的多个命令组合,可以很方便的进行一些判断 数据流重定向 数据流重定向:大致上的意思就是本该输出到屏幕上的数 ...

  9. 【shell编程基础3】shell编程的组合应用之二:管道及其命令

    预备知识: 管道:它是一个单向的,可以把前一个的数据输出导向到下一个命令的工具,这样可以实现多个命令组合处理一套数据. 它的符号是  "|"    管道只能处理经过前面一个命令传过 ...

随机推荐

  1. Opencv混合高斯模型前景分离

    #include "stdio.h" #include "string.h" #include "iostream" #include &q ...

  2. JAVA_HOME path classpath 以及cmd编译运行java代码

    JAVA_HOME PATH CLASSPATH 三者的区别:安装完jdk之后,首先在环境变量里面添加JAVA_HOME ,例如安装路径为C:\Program Files\Java\jdk1.6.0_ ...

  3. Codeforces 727 F. Polycarp's problems

    Description 有一个长度为 \(n\) 有正负权值的序列,你一开始有一个值,每次到一个权值就加上,最少需要删掉多少数值才能到序列末尾.\(n \leqslant 750,m \leqslan ...

  4. NOI2009 诗人小G

    Sol 决策单调性+二分 传说中的四边形不等式...其实做了这道题还是不会... 证明简直吃屎//// 贴个传送门这里有部分分做法还有决策单调性的证明 byvoid ISA tell me that ...

  5. py代码模糊工具

    https://github.com/astrand/pyobfuscate 使用方法,调用下图文件即可,方法 python  pyobfuscate.py your_code.py 即可输出在cmd ...

  6. 转:TopN推荐系统——推荐的实现与推荐效果的评价指标

    转自:用户推荐系统_python 代码-豆瓣书籍:项亮的<推荐系统实践> import random import math class UserBasedCF: def __init__ ...

  7. linux中shell截取字符串方法总结

    截取字符串的方法一共有八种,主要为以下方法 shell中截取字符串的方法有很多中, ${expression}一共有9种使用方法. ${parameter:-word} ${parameter:=wo ...

  8. 【GoLang】GoLang 遍历 map、slice、array方法

    代码示例: map1 := make(map[string]string) map1["a"] = "AAA" map1["b"] = &q ...

  9. git中找回丢失的对象

    本文转载自:http://gitbook.liuhui998.com/5_9.html 译者注: 原书这里只有两个链接: Recovering Lost Commits Blog Post,Recov ...

  10. Node.js——Async

    一:流程控制 为了适应异步编程,减少回调的嵌套,我尝试了很多库.最终觉得还是async最靠谱. 地址:https://github.com/caolan/async Async的内容分为三部分: 流程 ...