1. shell编程系列6--shell中的函数
  2.  
  3. .函数介绍
  4.  
  5. linux shell中的函数和大多数编程语言中的函数一样
  6. 将相似的任务或者代码封装到函数中,供其他地方调用
  7.  
  8. 语法格式
  9.  
  10. 第一种格式
  11.  
  12. name()
  13. {
  14. command1
  15. command2
  16. ......
  17. commondn
  18. }
  19.  
  20. 第二种格式
  21.  
  22. function name
  23. {
  24. command1
  25. command2
  26. ......
  27. commondn
  28. }
  29.  
  30. .调用函数
  31.  
  32. 直接使用函数名调用,可以将其想象成shell中的一条命令
  33.  
  34. 函数内部可以直接使用参数 $$...$n
  35.  
  36. 调用函数:function_name $ $
  37.  
  38. shell终端中定义函数
  39.  
  40. [root@es01 shell]# test()
  41. > {
  42. > echo "test function"
  43. > }
  44. [root@es01 shell]#
  45. [root@es01 shell]# test
  46. test function
  47.  
  48. [root@es01 shell]# function greeting
  49. > {
  50. > echo "hello,zhangsan"
  51. > }
  52. [root@es01 shell]# greeting
  53. hello,zhangsan
  54.  
  55. 练习;nginx的守护进程
  56.  
  57. [root@es01 shell]# cat nginx_daemon.sh
  58. #!/bin/bash
  59. #
  60. # 获取脚本子进程的pid,如果脚本名称中带nginx,也会当成nginx进程
  61. this_pid=$$
  62.  
  63. while true
  64. do
  65. ps -ef | grep nginx | grep -v grep | grep -v $this_pid &> /dev/null
  66.  
  67. if [ $? -eq ];then
  68. echo "Nginx is running well"
  69. sleep
  70. else
  71. systemctl start nginx
  72. echo "Nginx is down,Start it..."
  73. fi
  74. done
  75.  
  76. [root@es01 shell]# nohup sh nginx_daemon.sh &
  77. []
  78. [root@es01 shell]# nohup: ignoring input and appending output to nohup.out
  79.  
  80. [root@es01 shell]# tail -f nohup.out
  81. Nginx is running well
  82. Nginx is running well
  83. Nginx is running well
  84. Nginx is running well
  85.  
  86. .向函数传递参数
  87.  
  88. 高级语言传参
  89.  
  90. int example_1(int arg1,int arg2)
  91. {
  92. arg1=arg2
  93. ...
  94. ...
  95. return null
  96. }
  97.  
  98. 高级语言函数调用
  99.  
  100. int num1=;
  101. int num2=;
  102.  
  103. 调用函数形式一:int num3=example_1(num1,num2);
  104. 调用函数形式二:
  105. int num4;
  106. num4=example_1(num1,num2);
  107.  
  108. shell中传参
  109.  
  110. function name
  111. {
  112. echo "Hello $1"
  113. echo "Hello $2"
  114. }
  115.  
  116. shell中函数调用
  117. 函数调用:name Lily Allen
  118.  
  119. [root@es01 shell]# function greeting
  120. > {
  121. > echo "hello zhangsan"
  122. > }
  123. [root@es01 shell]# greeting
  124. hello zhangsan
  125.  
  126. # 调用参数
  127. [root@es01 shell]# function greeeting
  128. > {
  129. > echo "hello $1"
  130. > }
  131. [root@es01 shell]# greeeting jack
  132. hello jack
  133. [root@es01 shell]# greeeting tom
  134. hello tom
  135.  
  136. 向函数传递参数:
  137. 函数传参和给脚本传参类似,都是使用$ $ $ $ $ $ $7这种方式
  138.  
  139. 例子1
  140. 需求描述:写一个脚本,该脚本可以实现计算器的功能,可以进行+-*/四种运算。
  141. 例如:sh calculate.sh + | sh calucate.sh -
  142.  
  143. [root@es01 shell]# cat calucate.sh
  144. #!/bin/bash
  145. #
  146.  
  147. function calcu
  148. {
  149. case $ in
  150. +)
  151. echo "`expr $1 + $3`"
  152. ;;
  153. -)
  154. echo "`expr $1 - $3`"
  155. ;;
  156. \*)
  157. echo "`expr $1 \* $3`"
  158. ;;
  159. /)
  160. echo "`expr $1 / $3`"
  161. ;;
  162. esac
  163. }
  164.  
  165. calcu $ $ $
  166.  
  167. [root@es01 shell]# sh calucate.sh +
  168.  
  169. [root@es01 shell]# sh calucate.sh -
  170. -
  171.  
  172. .函数的返回值
  173.  
  174. 方法一 return
  175. 方法二 echo
  176.  
  177. 使用return返回值
  178. 使用return返回值,只能返回1-255的整数
  179.  
  180. 函数使用return返回值,通常只是用来供其他地方调用获取状态,因此通常仅返回010表示成功,1表示失败
  181.  
  182. # 判断nginx进程是否存在
  183. [root@es01 shell]# cat nginx.sh
  184. #!/bin/bash
  185. #
  186.  
  187. this_pid=$$
  188.  
  189. function is_nginx_running
  190. {
  191. ps -ef | grep nginx | grep -v grep | grep -v $this_pid &> /dev/null
  192. if [ $? -eq ];then
  193. return
  194. else
  195. return
  196. fi
  197. }
  198.  
  199. is_nginx_running && echo "nginx is running" || echo "nginx is stopped"
  200.  
  201. [root@es01 shell]# sh nginx.sh
  202. nginx is running
  203. [root@es01 shell]# systemctl stop nginx
  204. [root@es01 shell]# sh nginx.sh
  205. nginx is stopped
  206. [root@es01 shell]# sh -x nginx.sh
  207. + this_pid=
  208. + is_nginx_running
  209. + ps -ef
  210. + grep nginx
  211. + grep -v grep
  212. + grep -v
  213. + '[' -eq ']'
  214. + return
  215. + echo 'nginx is stopped'
  216. nginx is stopped
  217.  
  218. 使用echo返回值
  219.  
  220. 使用echo可以返回任何字符串结果
  221.  
  222. 通常用于返回数据,比如一个字符串值或者列表值
  223.  
  224. # 获取系统中的用户
  225. [root@es01 shell]# cat get_sys_user.sh
  226. #!/bin/bash
  227. #
  228.  
  229. # 获取系统所有的用户名
  230. function get_users
  231. {
  232. users=`cat /etc/passwd | cut -d: -f1`
  233. echo $users
  234. }
  235.  
  236. # 定义一个变量将获取用户列表赋值给这个变量
  237. user_list=`get_users`
  238.  
  239. index=
  240. for u in $user_list
  241. do
  242. echo "the $index user is : $u"
  243. index=$(($index+))
  244. done
  245.  
  246. [root@es01 shell]# sh get_sys_user.sh
  247. the user is : root
  248. the user is : bin
  249. the user is : daemon
  250. the user is : adm
  251. the user is : lp
  252. the user is : sync
  253. the user is : shutdown
  254. the user is : halt
  255. the user is : mail
  256. the user is : operator
  257. the user is : games
  258. the user is : ftp
  259. the user is : nobody
  260. the user is : systemd-network
  261. the user is : dbus
  262. the user is : polkitd
  263. the user is : sshd
  264. the user is : postfix
  265. the user is : ajie
  266. the user is : chrony
  267. the user is : elasticsearch
  268. the user is : nginx
  269.  
  270. .shell函数中的局部变量和全局变量
  271.  
  272. 不做特殊声明,shell中变量都是全局变量
  273.  
  274. 大型脚本程序函数中慎用全局变量
  275.  
  276. 局部变量:
  277.  
  278. 在函数内部定义局部变量时,使用local关键字
  279.  
  280. 函数内和函数外若同时存在变量,函数内部变量会覆盖外部变量
  281.  
  282. #
  283. [root@es01 shell]# cat var.sh
  284. #!/bin/bash
  285. #
  286.  
  287. var1="Hello world"
  288.  
  289. function test
  290. {
  291. var2=
  292. }
  293.  
  294. echo $var1
  295. echo $var2
  296.  
  297. # 因为函数test没有被调用,所以变量 $var2 没有生效,为空
  298. [root@es01 shell]# sh var.sh
  299. Hello world
  300.  
  301. [root@es01 shell]#
  302.  
  303. # 调用test函数后,$var2就变成了全局变量
  304. [root@es01 shell]# cat var.sh
  305. #!/bin/bash
  306. #
  307.  
  308. var1="Hello world"
  309.  
  310. function test
  311. {
  312. var2=
  313. }
  314.  
  315. echo $var1
  316. echo $var2
  317.  
  318. test # 调用test函数后,$var2就变成了全局变量
  319.  
  320. echo $var1
  321. echo $var2
  322.  
  323. [root@es01 shell]# sh var.sh
  324. Hello world
  325.  
  326. Hello world
  327.  
  328. # 在函数中也可以调用全局变量
  329. [root@es01 shell]# cat var.sh
  330. #!/bin/bash
  331. #
  332.  
  333. var1="Hello world"
  334.  
  335. function test
  336. {
  337. var2=
  338. }
  339.  
  340. echo $var1
  341. echo $var2
  342.  
  343. test
  344.  
  345. echo $var1
  346. echo $var2
  347.  
  348. function test1
  349. {
  350. echo $var2
  351. }
  352.  
  353. test1
  354. [root@es01 shell]# sh var.sh
  355. Hello world
  356.  
  357. Hello world
  358.  
  359. # 如果函数中声明了局部变量,当函数执行完成后局部变量就会被销毁
  360. [root@es01 shell]# cat var.sh
  361. #!/bin/bash
  362. #
  363.  
  364. var1="Hello world"
  365.  
  366. function test
  367. {
  368. local var2= # 局部变量,只在函数内部生效,生命周期只在函数内部
  369. }
  370.  
  371. test
  372.  
  373. echo $var1
  374. echo $var2
  375.  
  376. [root@es01 shell]# sh var.sh
  377. Hello world
  378.  
  379. [root@es01 shell]#
  380.  
  381. .函数库
  382.  
  383. 为什么需要定义函数库?
  384.  
  385. 经常使用的重复代码可以封装成函数文件,功能函数,程序工具
  386.  
  387. 函数库文件一般不直接执行,而是由其他脚本调用
  388.  
  389. 函数库示例:
  390.  
  391. 定义一个函数库,该函数库实现以下几个函数:
  392. .加法函数add
  393. add
  394. .减法函数reduce
  395. reduce
  396. .乘法函数multiple
  397. multiple
  398. .除法函数divide
  399. divide
  400. .打印系统运行情况的函数sys_load,该函数可以显示内存运行情况,磁盘使用情况
  401.  
  402. # 定义库函数
  403. [root@es01 shell]# cat lib/base_function
  404. function add
  405. {
  406. echo "`expr $1 + $2`"
  407. }
  408.  
  409. function reduce
  410. {
  411. echo "`expr $1 - $2`"
  412. }
  413.  
  414. function multiple
  415. {
  416. echo "`expr $1 \* $2`"
  417. }
  418.  
  419. function divide
  420. {
  421. echo "`expr $1 / $2`"
  422. }
  423.  
  424. function sys_load
  425. {
  426. echo "Memory info"
  427. echo
  428. free -m
  429. echo
  430. echo "Disk Usage"
  431. echo
  432. df -h
  433. echo
  434. }
  435.  
  436. # 测试库函数
  437. [root@es01 shell]# . lib/base_function
  438. [root@es01 shell]# sys_load
  439. Memory info
  440.  
  441. total used free shared buff/cache available
  442. Mem:
  443. Swap:
  444.  
  445. Disk Usage
  446.  
  447. Filesystem Size Used Avail Use% Mounted on
  448. /dev/mapper/centos-root 49G .3G 46G % /
  449. devtmpfs .9G .9G % /dev
  450. tmpfs .9G .9G % /dev/shm
  451. tmpfs .9G 12M .9G % /run
  452. tmpfs .9G .9G % /sys/fs/cgroup
  453. /dev/sda1 297M 125M 172M % /boot
  454. /dev/mapper/centos-data 49G 33M 49G % /data
  455. tmpfs 378M 378M % /run/user/
  456.  
  457. # 调用库函数
  458. [root@es01 shell]# cat calculate.sh
  459. #!/bin/bash
  460. #
  461.  
  462. # 引入库函数,写绝对路径避免出错
  463. . /data/shell/lib/base_function
  464.  
  465. add
  466.  
  467. reduce
  468.  
  469. multiple
  470.  
  471. divide
  472.  
  473. 经验之谈:
  474. 库文件名的后缀是任意的,但一般使用.lib
  475.  
  476. 库文件通常没有可执行权限
  477.  
  478. 库文件无需和脚本放在同级目录,只需要在脚本中引用时指定
  479.  
  480. 第一行一般使用#!/bin/echo,输出警告信息,避免用户执行

shell编程系列6--shell中的函数的更多相关文章

  1. shell编程系列1--shell脚本中的变量替换

    shell编程系列1--shell脚本中的变量替换 变量替换总结: .${变量#匹配规则} # 从头开始匹配,最短删除 .${变量##匹配规则} # 从头开始匹配,最长删除(贪婪模式) .${变量%匹 ...

  2. shell编程系列19--文本处理三剑客之awk中的字符串函数

    shell编程系列19--文本处理三剑客之awk中的字符串函数 字符串函数对照表(上) 函数名 解释 函数返回值 length(str) 计算字符串长度 整数长度值 index(str1,str2) ...

  3. shell编程系列24--shell操作数据库实战之利用shell脚本将文本数据导入到mysql中

    shell编程系列24--shell操作数据库实战之利用shell脚本将文本数据导入到mysql中 利用shell脚本将文本数据导入到mysql中 需求1:处理文本中的数据,将文本中的数据插入到mys ...

  4. shell编程系列21--文本处理三剑客之awk中数组的用法及模拟生产环境数据统计

    shell编程系列21--文本处理三剑客之awk中数组的用法及模拟生产环境数据统计 shell中的数组的用法: shell数组中的下标是从0开始的 array=("Allen" & ...

  5. shell编程系列18--文本处理三剑客之awk动作中的条件及if/while/do while/for循环语句

    shell编程系列18--文本处理三剑客之awk动作中的条件及if/while/do while/for循环语句条件语句 if(条件表达式) 动作1 else if(条件表达式) 动作2 else 动 ...

  6. shell编程系列17--文本处理三剑客之awk动作中的表达式用法

    shell编程系列17--文本处理三剑客之awk动作中的表达式用法 awk动作表达式中的算数运算符 awk动作中的表达式用法总结: 运算符 含义 + 加 - 减 * 乘 / 除 % 模 ^或** 乘方 ...

  7. shell编程系列11--文本处理三剑客之sed利用sed删除文本中的内容

    shell编程系列11--文本处理三剑客之sed利用sed删除文本中的内容 删除命令对照表 命令 含义 1d 删除第一行内容 ,10d 删除1行到10行的内容 ,+5d 删除10行到16行的内容 /p ...

  8. shell编程系列7--shell中常用的工具find、locate、which、whereis

    shell编程系列7--shell中常用的工具find.locate.which.whereis .文件查找之find命令 语法格式:find [路径] [选项] [操作] 选项 -name 根据文件 ...

  9. C#)Windows Shell 编程系列5 - 获取图标

    原文 C#)Windows Shell 编程系列5 - 获取图标 (本系列文章由柠檬的(lc_mtt)原创,转载请注明出处,谢谢-) 接上一节:(C#)Windows Shell 编程系列4 - 上下 ...

随机推荐

  1. 如何使用MCUXpresso IDE创建一个Cortex-M工程

    拿到Cortex-M开发板之后,就可以开始使用MCUXpresso IDE上手入门.在这个教程中,我们将详细介绍如何基于CMSIS(Cortex微控制器软件接口标准)在MCUXpresso IDE中为 ...

  2. 清除PLSQL Developer访问数据库连接的历史记录

    1.C盘下 路径: C:\Users\JourneyOfFlower\AppData\Roaming\PLSQL Developer 12\Preferences\JourneyOfFlower\ J ...

  3. TODO 竞品分析方法——关于导航评测的一些笔记

    参考:移动App性能评测与优化 chapter4讲了地图怎么测,虽然不测地图,但是里面有关竞品分析的部分写得非常好,很多解决方案真的很精彩.记录一下. 我之前的竞品分析测试,通常是很简单的竞品数据层面 ...

  4. IOT设备通讯,MQTT物联网协议,MQTTnet

    一.IOT设备的特性 硬件能力差(存储能力基本只有几MB,CPU频率低连使用HTTP请求都很奢侈) 系统千差万别(Brillo,mbedOS,RIOT等) 如使用电池供电,电量消耗敏感 如果是小设备, ...

  5. 2019-2020-1 20199312《Linux内核原理与分析》第十二周作业

    实验背景 2014年9月24日,Bash中发现了一个严重漏洞shellshock,该漏洞可用于许多系统,并且既可以远程也可以在本地触发.在本实验中,学生需要亲手重现攻击来理解该漏洞,并回答一些问题. ...

  6. select下拉选中显示对应的div隐藏不相关的div

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  7. 运算符 & | ^ ~ >> << 讲解

    字节”是byte,“位”是bit :1 byte = 8 bit : char 在java中是2个字节.java采用unicode,2个字节(16位)来表示一个字符. char 16位2个字节 byt ...

  8. 获取当前主题颜色 Flutter

    通过context获取当前主题颜色   Theme.of(context).accentColor

  9. learning java Random 和 ThreadLocalRandom类

    var rand = new Random(); System.out.println(rand.nextBoolean()); System.out.println(rand.nextInt()); ...

  10. 28、对多次使用的RDD进行持久化或Checkpoint

    一.图解 二.说明 如果程序中,对某一个RDD,基于它进行了多次transformation或者action操作.那么就非常有必要对其进行持久化操作,以避免对一个RDD反复进行计算. 此外,如果要保证 ...