shell编程系列6--shell中的函数
shell编程系列6--shell中的函数 .函数介绍 linux shell中的函数和大多数编程语言中的函数一样
将相似的任务或者代码封装到函数中,供其他地方调用 语法格式 第一种格式 name()
{
command1
command2
......
commondn
} 第二种格式 function name
{
command1
command2
......
commondn
} .调用函数 直接使用函数名调用,可以将其想象成shell中的一条命令 函数内部可以直接使用参数 $、$...$n 调用函数:function_name $ $ shell终端中定义函数 [root@es01 shell]# test()
> {
> echo "test function"
> }
[root@es01 shell]#
[root@es01 shell]# test
test function [root@es01 shell]# function greeting
> {
> echo "hello,zhangsan"
> }
[root@es01 shell]# greeting
hello,zhangsan 练习;nginx的守护进程 [root@es01 shell]# cat nginx_daemon.sh
#!/bin/bash
#
# 获取脚本子进程的pid,如果脚本名称中带nginx,也会当成nginx进程
this_pid=$$ while true
do
ps -ef | grep nginx | grep -v grep | grep -v $this_pid &> /dev/null if [ $? -eq ];then
echo "Nginx is running well"
sleep
else
systemctl start nginx
echo "Nginx is down,Start it..."
fi
done [root@es01 shell]# nohup sh nginx_daemon.sh &
[]
[root@es01 shell]# nohup: ignoring input and appending output to ‘nohup.out’ [root@es01 shell]# tail -f nohup.out
Nginx is running well
Nginx is running well
Nginx is running well
Nginx is running well .向函数传递参数 高级语言传参 int example_1(int arg1,int arg2)
{
arg1=arg2
...
...
return null
} 高级语言函数调用 int num1=;
int num2=; 调用函数形式一:int num3=example_1(num1,num2);
调用函数形式二:
int num4;
num4=example_1(num1,num2); shell中传参 function name
{
echo "Hello $1"
echo "Hello $2"
} shell中函数调用
函数调用:name Lily Allen [root@es01 shell]# function greeting
> {
> echo "hello zhangsan"
> }
[root@es01 shell]# greeting
hello zhangsan # 调用参数
[root@es01 shell]# function greeeting
> {
> echo "hello $1"
> }
[root@es01 shell]# greeeting jack
hello jack
[root@es01 shell]# greeeting tom
hello tom 向函数传递参数:
函数传参和给脚本传参类似,都是使用$ $ $ $ $ $ $7这种方式 例子1:
需求描述:写一个脚本,该脚本可以实现计算器的功能,可以进行+-*/四种运算。
例如:sh calculate.sh + | sh calucate.sh - [root@es01 shell]# cat calucate.sh
#!/bin/bash
# function calcu
{
case $ in
+)
echo "`expr $1 + $3`"
;;
-)
echo "`expr $1 - $3`"
;;
\*)
echo "`expr $1 \* $3`"
;;
/)
echo "`expr $1 / $3`"
;;
esac
} calcu $ $ $ [root@es01 shell]# sh calucate.sh + [root@es01 shell]# sh calucate.sh -
- .函数的返回值 方法一 return
方法二 echo 使用return返回值
使用return返回值,只能返回1-255的整数 函数使用return返回值,通常只是用来供其他地方调用获取状态,因此通常仅返回0或1;0表示成功,1表示失败 # 判断nginx进程是否存在
[root@es01 shell]# cat nginx.sh
#!/bin/bash
# this_pid=$$ function is_nginx_running
{
ps -ef | grep nginx | grep -v grep | grep -v $this_pid &> /dev/null
if [ $? -eq ];then
return
else
return
fi
} is_nginx_running && echo "nginx is running" || echo "nginx is stopped" [root@es01 shell]# sh nginx.sh
nginx is running
[root@es01 shell]# systemctl stop nginx
[root@es01 shell]# sh nginx.sh
nginx is stopped
[root@es01 shell]# sh -x nginx.sh
+ this_pid=
+ is_nginx_running
+ ps -ef
+ grep nginx
+ grep -v grep
+ grep -v
+ '[' -eq ']'
+ return
+ echo 'nginx is stopped'
nginx is stopped 使用echo返回值 使用echo可以返回任何字符串结果 通常用于返回数据,比如一个字符串值或者列表值 # 获取系统中的用户
[root@es01 shell]# cat get_sys_user.sh
#!/bin/bash
# # 获取系统所有的用户名
function get_users
{
users=`cat /etc/passwd | cut -d: -f1`
echo $users
} # 定义一个变量将获取用户列表赋值给这个变量
user_list=`get_users` index=
for u in $user_list
do
echo "the $index user is : $u"
index=$(($index+))
done [root@es01 shell]# sh get_sys_user.sh
the user is : root
the user is : bin
the user is : daemon
the user is : adm
the user is : lp
the user is : sync
the user is : shutdown
the user is : halt
the user is : mail
the user is : operator
the user is : games
the user is : ftp
the user is : nobody
the user is : systemd-network
the user is : dbus
the user is : polkitd
the user is : sshd
the user is : postfix
the user is : ajie
the user is : chrony
the user is : elasticsearch
the user is : nginx .shell函数中的局部变量和全局变量 不做特殊声明,shell中变量都是全局变量 大型脚本程序函数中慎用全局变量 局部变量: 在函数内部定义局部变量时,使用local关键字 函数内和函数外若同时存在变量,函数内部变量会覆盖外部变量 #
[root@es01 shell]# cat var.sh
#!/bin/bash
# var1="Hello world" function test
{
var2=
} echo $var1
echo $var2 # 因为函数test没有被调用,所以变量 $var2 没有生效,为空
[root@es01 shell]# sh var.sh
Hello world [root@es01 shell]# # 调用test函数后,$var2就变成了全局变量
[root@es01 shell]# cat var.sh
#!/bin/bash
# var1="Hello world" function test
{
var2=
} echo $var1
echo $var2 test # 调用test函数后,$var2就变成了全局变量 echo $var1
echo $var2 [root@es01 shell]# sh var.sh
Hello world Hello world # 在函数中也可以调用全局变量
[root@es01 shell]# cat var.sh
#!/bin/bash
# var1="Hello world" function test
{
var2=
} echo $var1
echo $var2 test echo $var1
echo $var2 function test1
{
echo $var2
} test1
[root@es01 shell]# sh var.sh
Hello world Hello world # 如果函数中声明了局部变量,当函数执行完成后局部变量就会被销毁
[root@es01 shell]# cat var.sh
#!/bin/bash
# var1="Hello world" function test
{
local var2= # 局部变量,只在函数内部生效,生命周期只在函数内部
} test echo $var1
echo $var2 [root@es01 shell]# sh var.sh
Hello world [root@es01 shell]# .函数库 为什么需要定义函数库? 经常使用的重复代码可以封装成函数文件,功能函数,程序工具 函数库文件一般不直接执行,而是由其他脚本调用 函数库示例: 定义一个函数库,该函数库实现以下几个函数:
.加法函数add
add
.减法函数reduce
reduce
.乘法函数multiple
multiple
.除法函数divide
divide
.打印系统运行情况的函数sys_load,该函数可以显示内存运行情况,磁盘使用情况 # 定义库函数
[root@es01 shell]# cat lib/base_function
function add
{
echo "`expr $1 + $2`"
} function reduce
{
echo "`expr $1 - $2`"
} function multiple
{
echo "`expr $1 \* $2`"
} function divide
{
echo "`expr $1 / $2`"
} function sys_load
{
echo "Memory info"
echo
free -m
echo
echo "Disk Usage"
echo
df -h
echo
} # 测试库函数
[root@es01 shell]# . lib/base_function
[root@es01 shell]# sys_load
Memory info total used free shared buff/cache available
Mem:
Swap: Disk Usage Filesystem Size Used Avail Use% Mounted on
/dev/mapper/centos-root 49G .3G 46G % /
devtmpfs .9G .9G % /dev
tmpfs .9G .9G % /dev/shm
tmpfs .9G 12M .9G % /run
tmpfs .9G .9G % /sys/fs/cgroup
/dev/sda1 297M 125M 172M % /boot
/dev/mapper/centos-data 49G 33M 49G % /data
tmpfs 378M 378M % /run/user/ # 调用库函数
[root@es01 shell]# cat calculate.sh
#!/bin/bash
# # 引入库函数,写绝对路径避免出错
. /data/shell/lib/base_function add reduce multiple divide 经验之谈:
库文件名的后缀是任意的,但一般使用.lib 库文件通常没有可执行权限 库文件无需和脚本放在同级目录,只需要在脚本中引用时指定 第一行一般使用#!/bin/echo,输出警告信息,避免用户执行
shell编程系列6--shell中的函数的更多相关文章
- shell编程系列1--shell脚本中的变量替换
shell编程系列1--shell脚本中的变量替换 变量替换总结: .${变量#匹配规则} # 从头开始匹配,最短删除 .${变量##匹配规则} # 从头开始匹配,最长删除(贪婪模式) .${变量%匹 ...
- shell编程系列19--文本处理三剑客之awk中的字符串函数
shell编程系列19--文本处理三剑客之awk中的字符串函数 字符串函数对照表(上) 函数名 解释 函数返回值 length(str) 计算字符串长度 整数长度值 index(str1,str2) ...
- shell编程系列24--shell操作数据库实战之利用shell脚本将文本数据导入到mysql中
shell编程系列24--shell操作数据库实战之利用shell脚本将文本数据导入到mysql中 利用shell脚本将文本数据导入到mysql中 需求1:处理文本中的数据,将文本中的数据插入到mys ...
- shell编程系列21--文本处理三剑客之awk中数组的用法及模拟生产环境数据统计
shell编程系列21--文本处理三剑客之awk中数组的用法及模拟生产环境数据统计 shell中的数组的用法: shell数组中的下标是从0开始的 array=("Allen" & ...
- shell编程系列18--文本处理三剑客之awk动作中的条件及if/while/do while/for循环语句
shell编程系列18--文本处理三剑客之awk动作中的条件及if/while/do while/for循环语句条件语句 if(条件表达式) 动作1 else if(条件表达式) 动作2 else 动 ...
- shell编程系列17--文本处理三剑客之awk动作中的表达式用法
shell编程系列17--文本处理三剑客之awk动作中的表达式用法 awk动作表达式中的算数运算符 awk动作中的表达式用法总结: 运算符 含义 + 加 - 减 * 乘 / 除 % 模 ^或** 乘方 ...
- shell编程系列11--文本处理三剑客之sed利用sed删除文本中的内容
shell编程系列11--文本处理三剑客之sed利用sed删除文本中的内容 删除命令对照表 命令 含义 1d 删除第一行内容 ,10d 删除1行到10行的内容 ,+5d 删除10行到16行的内容 /p ...
- shell编程系列7--shell中常用的工具find、locate、which、whereis
shell编程系列7--shell中常用的工具find.locate.which.whereis .文件查找之find命令 语法格式:find [路径] [选项] [操作] 选项 -name 根据文件 ...
- C#)Windows Shell 编程系列5 - 获取图标
原文 C#)Windows Shell 编程系列5 - 获取图标 (本系列文章由柠檬的(lc_mtt)原创,转载请注明出处,谢谢-) 接上一节:(C#)Windows Shell 编程系列4 - 上下 ...
随机推荐
- Makefile学习一
上次随着信号学习告一段落,也标志着linux系统编程相关的知识学完了,而学了这么多知识点,是需要用一个综合的项目来将其进行串起来的,这样学习的技术才会不那么空洞,所以接下来会以一个实际例子来综合运用下 ...
- ModelForm的简单使用-注册用modelform编写
1.前端的ajax代码不用改动 2.modelform,在原来基础上稍作改动 from django import forms from app01.models import UserInfo fr ...
- 任何人都适合的常用Chrome插件(工欲善其事必先利其器)
1.划词翻译 介绍链接:http://t.cn/RqpoGU4 下载地址: Chrome 应用商店(请翻墙). 下载 .crx 安装包手动安装 功能如下: - 支持几乎所有语言的翻译与阅读,并且同时支 ...
- 五.Protobuf3 枚举
Protobuf3 枚举 定义消息类型时,您可能希望它的一个字段有一个预定义的值列表.例如,假设您希望为每个SearchRequest添加一个corpus字段,其中语料库可以是UNIVERSAL.WE ...
- python不使用系统库中的排序方法判断一个数组是否是有序数组
2. 给定一组整数, 已知其每两个数都互不相同,判断这些数字是否能排成一个有序的数组? 例:li = [1,3,4,2] 是有续的 可以排序为li =[1,2,3,4] li = [2,4,6,8] ...
- 016_Python3 函数
函数是组织好的,可重复使用的,用来实现单一,或相关联功能的代码段. 函数能提高应用的模块性,和代码的重复利用率.你已经知道Python提供了许多内建函数,比如print().但你也可以自己创建函数,这 ...
- (尚003).Vue_模板语法
1.双大括号表达式 2.指令一:强制数据绑定 3.指令二;绑定事件监听 test003.html<!DOCTYPE html><html lang="en"> ...
- ico图标和制作网站(比特虫)
1.怎么找到网站的图标: 网址/favicon.ico 代码引入 <link rel="icon" href="//www.jd.com/favicon.ico&q ...
- ipkg-nas
http://pkg.entware.net/binaries/x86-64/ https://forum.synology.com/enu/viewtopic.php?t=95346 http:// ...
- log4g:站在巨人的头上实现一个可配置的Go日志库
更多精彩博文,欢迎访问我的个人博客 前言 本人Java程序员一枚,眼看着这几年Go的势头不错,本着技多不压身的原则,也随大流慢慢学习.不得不说Go其实跟Java差别还是挺大的,毕竟习惯了面向对象的思想 ...