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. 通过消息总线Spring Cloud Bus实现配置文件刷新(使用Kafka或RocketMQ)

    如果需要客户端获取到最新的配置信息需要执行refresh,我们可以利用webhook的机制每次提交代码发送请求来刷新客户端,当客户端越来越多的时候,需要每个客户端都执行一遍,这种方案就不太适合了.使用 ...

  2. maven的基础入门

    Maven是Java世界中一个很好使的项目管理工具,关于[好使]这个特性从项目的使用量上就能体现出来,虽然说现在有更好使的Gradle,但是Maven的地位也不会那么轻易被撼动,支持者还是多多. Ma ...

  3. Prometheus PromSQL 获取系统服务运行状态

    Prometheus PromSQL 获取系统服务运行状态 使用systemd收集器:--collector.systemd.unit-whitelist=".+" 从system ...

  4. 《 .NET并发编程实战》阅读指南 - 第10章

    先发表生成URL以印在书里面.等书籍正式出版销售后会公开内容.

  5. json传的解析,二维数据解析

    下载地址:https://share.weiyun.com/447eda75fdd46cb87f6622ecdce4c3ac

  6. 关于两个DIV之间的空白字符

    首先!!!!这个问题应该是去面试前端会经常问到的问题!!! 如,下面这个例子: <!DOCTYPE html> <html lang="zh-CN"> &l ...

  7. vue中路由拦截无限循环的情况

    router.beforeEach(async (to, from, next) => { if (token) { if (whiteList.indexOf(to.path) != -1) ...

  8. doucment的获取节点的信息

    document.activeElement 返回当前获取焦点元素 document.addEventListener() 向文档添加句柄 document.adoptNode(node) 从另外一个 ...

  9. iOS相关

    1. fastlane a collection of tools that help you automate building and releasing iOS and Android apps ...

  10. 【转】WPF 异步执行方法后对 UI 进行更新的几种方法

    使用 async/await 的情况: private async void Button_Click(object sender, RoutedEventArgs e) { (sender as B ...