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. linux 终端报错 Out of memory: Kill process[PID] [process name] score问题分析

    从Out of memory来看是内存超出了,后面的 Kill process[PID] [process name] score好像和进程有关了,下面我们就一起来看看linux 终端报错 Out o ...

  2. 项目中Ajax调用ashx页面中的Function的实战

    前台页面: 使用几个display=none的空间存储DropdownList中的值,点击Search Button后刷新页面再次给DropdownList赋值使用 <%@ Page Langu ...

  3. js里面的 InttoStr 和 StrtoInt

    javascript 字符串 和 数字的转换,话说好灵活,感觉回不去pascal了 int转换string: 1,var str=String(int); 2,num.toString(param) ...

  4. 【leetcode】Unique Binary Search Trees II

    Unique Binary Search Trees II Given n, generate all structurally unique BST's (binary search trees) ...

  5. windows下打开VMware虚拟机时提示内存不足的处理方法

    参考:http://thinkpig007.blog.51cto.com/971471/1589831 以管理员身份运行vmware.exe即可 错误的错误提示: Not enough physica ...

  6. iOS App Extensions 推荐文章

    写的非常不错,读完后,基本的extension的套路就清楚了,也是我们的园友写的,感谢他: http://www.cnblogs.com/xdream86/p/3855932.html 下面这个教程是 ...

  7. python模块名和文件名冲突解决

    对于python初学者,很容易练习到一个随机数生成的程序,代码如下: #!/usr/bin/python import random print(random.randint(12,20)) 这个小程 ...

  8. problem-record-mysql

    #!/bin/bash # # Update_Problem - updates problem record in database ################################ ...

  9. OKhttp的封装(下)

    OKhttpManager2.Class  请求工具类 package com.example.administrator.okhttp3; import android.os.Handler; im ...

  10. 【Git】笔记3

    来源:廖雪峰 远程仓库 远程仓库采用github 准备工作:创建远程仓库 1.创建一个github账号 2.在本地设置ssh,获取/home/user/.ssh/id_rsa.pub内容 3.在git ...