2.1.函数的定义和使用

函数基本使用

[root@VM_0_9_centos ~]# test()
> {}
-bash: syntax error near unexpected token `{}'
[root@VM_0_9_centos ~]# test() {}
-bash: syntax error near unexpected token `{}'
[root@VM_0_9_centos ~]# test()
> {
> echo "test function"
> }
[root@VM_0_9_centos ~]# test
test function
[root@VM_0_9_centos ~]# function greeting
> {
> echo "hello world"
> }
[root@VM_0_9_centos ~]# greeting
hello world
[root@VM_0_9_centos ~]#

实例一:写一个守护进程,nginx如果关闭自动开启

vim nginx_daemon.sh

#!/bin/bash
# #运行脚本的进程id,如果脚本名字有nginx字样,也需要把这个过滤掉
this_pid=$$ while true
do ps -ef |grep nginx |grep -v grep | grep -v $this_pid &> /dev/null if [ $? -eq 0 ];then
echo "Nginx is running well!"
sleep 3
else
systemctl start nginx
echo "Nginx is down,start it....."
fi
done

把这个脚本放到后台运行

nohup sh nginx_daemon.sh &

关闭后查看

tail -f nohup.out

2.2.向函数传递参数

shell中传参

function name
{
echo "hello $1"
echo "hello $2"
}

函数调用

name derek alice

举例

[root@VM_0_9_centos shell_learn]# function greeting
> {
> echo "Hello $1"
> }
[root@VM_0_9_centos shell_learn]#
[root@VM_0_9_centos shell_learn]# greeting derek
Hello derek
[root@VM_0_9_centos shell_learn]# greeting alice
Hello alice
[root@VM_0_9_centos shell_learn]#

2.3.函数的返回值

返回值的方式

方式一:return

方法二:echo

使用return返回值

  • 使用return返回值,只能返回1-255的整数
  • 函数使用return返回值,通常只是用来供其他地方调用 获取状态,因此通常仅返回0或1;0表示成功,1表示失败

使用echo返回值

  • 使用echo可以返回任何字符串结果
  • 通常用于返回数据,比如一个字符串值或者列表值

实例一

#!/bin/bash
# this_pid=$$ function is_nginx_running
{
ps -ef |grep nginx |grep -v grep | grep -v $this_pid &> /dev/null if [ $? -eq 0 ];then
return
else
return 1
fi
} is_nginx_running && echo "nginx is runnig...." || echo "nginx is stop!"

实例二:获取用户列表

#!/bin/bash
# function get_users
{
users=`cat /etc/passwd | cut -d: -f1`
echo $users
} user_list=`get_users` index=1 for user in $user_list
do
echo "The $index user is: $user"
index=$(($index+1))
done

2.4.局部变量和全局变量

全局变量

  • 不做特殊声明,shell中变量都是全局变量
  • 大型脚本程序函数中慎用全局变量

局部变量

  • 定义变量时,用local关键字
  • 函数内和函数外存在相同的变量,函数内部覆盖函数外部变量

2.5.函数库

函数库

  • 经常使用的重复代码封装成函数文件
  • 一般不直接执行,而是由其它脚本调用
  • 库文件名的后缀是任意的,但一般使用.lib
  • 库文件通常没有可执行选项
  • 库文件无需和脚本在同级目录,只需在脚本中引用时指定

2.shell编程-函数的高级用法的更多相关文章

  1. 1.shell编程-变量的高级用法

    1.1.变量替换 变量替换的六种形式 实例:非贪婪和贪婪的区别 从头部删除 [root@VM_0_9_centos shell_learn]# var_1="i love you,do yo ...

  2. Bash 脚本编程的一些高级用法

    概述 偶然间发现 man bash 上其实详细讲解了 shell 编程的语法,包括一些很少用却很实用的高级语法.就像发现了宝藏的孩子,兴奋莫名.于是参考man bash,结合自己的理解,整理出了这篇文 ...

  3. Shell编程中Shift的用法

    Shell编程中Shift的用法 位置参数可以用shift命令左移.比如shift 3表示原来的$4现在变成$1,原来的$5现在变成$2等等,原来的$1.$2.$3丢弃,$0不移动.不带参数的shif ...

  4. 详解shell编程中2>&1用法

    在使用 linux 命令或者 shell 编程时,这个用法常会遇到 2>&1 下面看一个命令示例,然后分析下他是如何工作的: ls foo > /dev/null 2>&am ...

  5. Shell编程中Shift的用法【转】

    本文转载自:http://www.cnblogs.com/image-eye/archive/2011/08/20/2147153.html Shell编程中Shift的用法 位置参数可以用shift ...

  6. shell 函数的高级用法

    函数介绍 linux shell中的函数和大多数编程语言中的函数一样 将相似的任务或者代码封装到函数中,供其他地方调用 语法格式 如何调用函数 shell终端中定义函数 [root@master da ...

  7. Shell 编程 函数

    本篇主要写一些shell脚本函数的使用. 函数调用 #!/bin/bash sum(){ s=`expr 2 + 3` echo $s } sum [root@localhost ~]# vim su ...

  8. Linux之shell编程函数使用

    linux shell 可以用户定义函数,然后在shell脚本中可以随便调用.下面说说它的定义方法,以及调用需要注意那些事项. 原文和作者一起讨论:http://www.cnblogs.com/int ...

  9. shell编程-函数(九)

    每种语言都有自己的函数,shell也不例外.支持函数,它可以将脚本程序划分成一个个相对独立的代码块,使代码的模块化,结构更加清晰,并有效地减少程序的代码量,提高代码的复用率. 函数格式 functio ...

随机推荐

  1. pkg_resources.DistributionNotFound: The 'pip==7.1.0' distribution was not found and is required by the application

    问题描述: Traceback (most recent call last): File "/usr/bin/pip", line 5, in <module> fr ...

  2. microbit之mpython的API

    附录:常用API函数汇总 一.显示 display.scroll("Hello, World!") 在micro:bit点阵上滚动显示Hello, World!,其中Hello, ...

  3. ascii码对照表(收藏)

    https://blog.csdn.net/yueyueniaolzp/article/details/82178954 十进制代码 十六进制代码 MCS 字符或缩写 DEC 多国字符名 ASCII ...

  4. WPF 高级篇 MVVM (MVVMlight) 依赖注入使用Messagebox

    原文:WPF 高级篇 MVVM (MVVMlight) 依赖注入使用Messagebox MVVMlight 实现依赖注入 把弹框功能 和接口功能注入到各个插件中 使用依赖注入 先把所有的ViewMo ...

  5. python 练习题:小明的成绩从去年的72分提升到了今年的85分,请计算小明成绩提升的百分点

    # -*- coding: utf-8 -*- # 小明的成绩从去年的72分提升到了今年的85分,请计算小明成绩提升的百分点,并用字符串格式化显示出'xx.x%',只保留小数点后1位. s1 = 72 ...

  6. C# vb .NET读取识别条形码线性条码gs1128

    gs1-128,ean-128是比较常见的条形码编码规则类型的一种.如何在C#,vb等.NET平台语言里实现快速准确读取该类型条形码呢?答案是使用SharpBarcode! SharpBarcode是 ...

  7. mvc控制器接收ajax传送的数据

    视图层中ajax传数据 $.ajax({ type: "post",//提交方式 data: { complay_arry: complay_arry, site_arry: si ...

  8. 【翻译】asp.net core 3.0基本概念

    这篇文章描述了开发asp.net core所需要掌握的基本概念. 原文地址:https://docs.microsoft.com/en-us/aspnet/core/fundamentals/?vie ...

  9. 并发编程需要场景化:CountDownLatch

  10. Tornado笔记

    helloworld Tornado特点一句话简介:Tornado是非阻塞式的Web服务器,速度非常快,每秒可以处理数以千计的链接,因此Tornado是实时Web服务的一个理想框架.Tornado因为 ...