2019独角兽企业重金招聘Python工程师标准>>>

20.17 shell中的函数

函数:就是把一段代码整理到了一个小单元中,并给这个小单元起一个名字,当用到这段代码时直接调用这个小单元的名字即可。
格式: function f_name() {
                      command
             }
函数必须要放在最前面;

示例1:

#!/bin/bash
inputt() {echo $1 $2 $# $0
}input 1 a b

注意:函数里面function可以省略,但f_name不要和系统env变量相同,如"for""input"一定要区分开来;
函数本事不会自动执行,只有你调用之后 才会生效;
$1:第一个参数;
$2:第二个参数;
$0:脚本名称;
$#:参数的数量(计算有多少参数);

脚本解析:
这个脚本并没有交互,预先脚本里面设定了参数;

[root@DasonCheng sbin]# cat funca.sh
#!/bin/bash
function inputt() {echo "The first par is $1"echo "The second par is $2"echo "The third par is $3"echo "The script name is $0"echo "The number of par is $#"
}
inputt 1 2 3 a b cde    ##这里就会调用上面的inputt函数;后面接的是参数!
[root@DasonCheng sbin]# sh funca.sh
The first par is 1
The second par is 2
The third par is 3
The script name is funca.sh
The number of par is 6    ##总共有6个参数;

示例2:

#!/bin/bash
sum() {s=$[$1+$2]echo $s
}
sum 1 2

脚本解析:

[root@DasonCheng sbin]# cat funca2.sh
#!/bin/bash
function sum () {s=$[$1+$2]echo "$1+$2=$s"
}
sum 1 10
[root@DasonCheng sbin]# sh funca2.sh
1+10=11

示例3:

#!/bin/bash
ip() {ifconfig |grep -A1 "$1 " |tail -1 |awk '{print $2}''
}
read -p "Please input the eth name: " e
myip=`ip $e`
echo "$e address is $myip"

提示:ip函数里面的命令不清楚的话,那就管道一个一个执行;可以换为以下命令:
[root@DasonCheng sbin]# ifconfig |grep -w -A1 "ens33:" |awk '/inet/ {print $2}'
[root@DasonCheng sbin]# ifconfig |grep -A1 "ens33: " |awk '/inet/ {print $2}'

脚本解析:

[root@DasonCheng sbin]# cat funca3.sh
#!/bin/bash
function ip() {ifconfig |grep -A1 "$e: " |awk '/inet/ {print $2}'
}
read -p "Please input the eth name:" e
myip=`ip $e`
echo "$e address is $myip"
[root@DasonCheng sbin]# sh funca3.sh
Please input the eth name:ens33
ens33 address is 192.168.60.11
[root@DasonCheng sbin]# sh funca3.sh
Please input the eth name:ens37
ens37 address is 192.168.80.128

脚本拓展:

需求:求出 当用户输入的不是系统内的网卡时或者没有输入时,应该怎样输出?(判断)

[root@DasonCheng sbin]# cat funct3.sh
#!/bin/bash
function ip() {ifconfig |grep -A1 "$e: " |awk '/inet/ {print $2}'
}
while :      ##这个while循环,我是用来判断输入值为的空或者网卡系统里面没有的!
do
read -p "Please input the eth name:" eif [ -z $e ]thenecho "Warnng: please input the eth name!"continuefi
sure=`ifconfig |grep "$e: " |wc -m`if [ $sure -le 0 ]thenecho "The $e is out of your system,please input again!"elsebreakfi
donemyip=`ip $e`
echo "$e address is $myip"
[root@DasonCheng sbin]# sh funct3.sh
Please input the eth name:
Warnng: please input the eth name!
Please input the eth name:asdf
The asdf is out of your system,please input again!
Please input the eth name:ens33
ens33 address is 192.168.60.11
[root@DasonCheng sbin]# sh funct3.sh
Please input the eth name:ens37
ens37 address is 192.168.80.128

20.18 shell中的数组

shell中的数组1:

定义数组 a=(1 2 3 4 5); echo ${a[@]}

  • echo ${#a[@]} 获取数组的元素个数
  • echo ${a[2]} 读取第三个元素,数组从0开始
  • echo ${a[*]} 等同于 ${a[@]} 显示整个数组

数组赋值

  • a[1]=100; echo ${a[@]}
  • a[5]=2; echo ${a[@]} 如果下标不存在则会自动添加一个元素

数组的删除

  • unset a; unset a[1]
[root@DasonCheng sbin]# a=(1 2 3 4 5)    ##定义一个数组;
[root@DasonCheng sbin]# echo ${a[@]}    ##显示所有数组 @和*效果一样;
1 2 3 4 5
[root@DasonCheng sbin]# echo ${a[*]}
1 2 3 4 5
[root@DasonCheng sbin]# echo ${#a[*]}    ##显示数组元素个数;
5
[root@DasonCheng sbin]# echo ${a[2]}    ##显示数组第2个元素值,数组是按0开始算的;
3

shell中的数组2:

数组分片:

  • a=(seq 1 5)
  • echo ${a[@]:0:3} 从第一个元素开始,截取3个
  • echo ${a[@]:1:4} 从第二个元素开始,截取4个
  • echo ${a[@]:0-3:2} 从倒数第3个元素开始,截取2个

数组替换:

  • echo ${a[@]/3/100}
  • a=(${a[@]/3/100})
[root@DasonCheng sbin]# a=(`seq 1 10`)
[root@DasonCheng sbin]# echo ${a[*]}
1 2 3 4 5 6 7 8 9 10
[root@DasonCheng ~]# echo ${a[*]:0:3}    ##从第一个元素开始,截取3个元素;
1 2 3
[root@DasonCheng ~]# echo ${a[*]:2:4}
3 4 5 6
[root@DasonCheng ~]# echo ${a[*]:0-3:2}     ##从倒数第三个元素开始,截取2个元素;
8 9
[root@DasonCheng ~]# a=(${a[*]/3/100})    ##数组替换:将元素3替换为100(并不是将第3个替换为100,需要区分开来)
[root@DasonCheng ~]# echo ${a[*]}
1 2 100 4 5 6 7 8 9 10
[root@DasonCheng ~]# echo ${a[*]/4/200}    ##将元素4替换为200,仅仅显示在屏幕上!
1 2 100 200 5 6 7 8 9 10
[root@DasonCheng ~]# echo ${a[*]}
1 2 100 4 5 6 7 8 9 10

20.19 告警系统需求分析

Shell项目-告警系统:

  • 需求:使用shell定制各种个性化告警工具,但需要统一化管理、规范化管理。
  • 思路:指定一个脚本包,包含主程序、子程序、配置文件、邮件引擎、输出日志等。
  • 主程序:作为整个脚本的入口,是整个系统的命脉。
  • 配置文件:是一个控制中心,用它来开关各个子程序,指定各个相关联的日志文件。
  • 子程序:这个才是真正的监控脚本,用来监控各个指标。
  • 邮件引擎:是由一个python程序来实现,它可以定义发邮件的服务器、发邮件人以及发件人密码
  • 输出日志:整个监控系统要有日志输出。

要求:我们的机器角色多种多样,但是所有机器上都要部署同样的监控系统,也就说所有机器不管什么角色,整个程序框架都是一致的,不同的地方在于根据不同的角色,定制不同的配置文件。

转载于:https://my.oschina.net/u/3651233/blog/1537691

20.17 shell中的函数相关推荐

  1. shell中的函数shell中的数组告警系统需求分析

    2019独角兽企业重金招聘Python工程师标准>>> 20.16/20.17 shell中的函数 函数的概念 函数就是把一段代码整理到了一个小单元中,并给这个小单元起一个名字,当用 ...

  2. shell中的函数、shell中的数组、 告警系统需求分析

    为什么80%的码农都做不了架构师?>>>    20.16/20.17 shell中的函数 shell中的函数 说明:函数就是子shell, 是一个代码段,定义完函数就可以引用它. ...

  3. shell中的函数及脚本调试方法

    1.函数格式 目的:将一些相对对立的代码变成函数,提供可读性和重用性,避免重复编写相同代码. 函数格式: 函数关键字:function可以省略,可不带任何参数; 符号{表示函数体的开始(可在函数名后单 ...

  4. Linux centosVMware shell中的函数、shell中的数组、

    一.shell中的函数 函数就是把一段代码整理到了一个小单元中,并给这个小单元起一个名字,当用到这段代码时直接调用这个小单元的名字即可. 格式: function _name() { command ...

  5. shell中模式匹配+函数+正则

    一.case语句 case语句为多选择语句.可以用case语句匹配一个值与一个模式,如果匹配成功,执行相 匹配的命令. case var in 定义变量:var代表是变量名 pattern 1) 模式 ...

  6. 在Shell中使用函数文件,引入文件

    需要编写一个较庞大的脚本时,可能会涉及许多函数.变量.这是通常建议将众多的函数.变量放入一个单独的脚本内.这样做的好处很明显,不用担心某个函数.变量是否已经被定义和使用,也不用频繁地定义.清除函数和变 ...

  7. idea shell 中的函数 跳转_SpringBoot项目打包+shell脚本部署实践,太有用了

    本篇和大家分享的是springboot打包并结合shell脚本命令部署,重点在分享一个shell程序启动工具,希望能便利工作: profiles指定不同环境的配置 maven-assembly-plu ...

  8. 小苏的Shell编程笔记之六--Shell中的函数

    http://xiaosu.blog.51cto.com/2914416/531247 Shell函数类似于Shell脚本,里面存放了一系列的指令,不过Shell的函数存在于内存,而不是硬盘文件,所以 ...

  9. 在shell中编写函数

    执行"nano function.sh"命令,创建新的shell脚本文件,名字为"function.sh". 编辑新创建的shell脚本文件"func ...

最新文章

  1. [C# Control] 仿RAR式进度条 (RarProgressBar)
  2. python函数def里面嵌套def,python菜鸟求问关于嵌套函数中作用域范围应该怎么理解?,python嵌套,直接上代码def l(l...
  3. 制药行业的GxP代表什么?
  4. cmake+qt+qtcreator的配置,解决Q_OBJECT的问题
  5. python discover()没有加载测试用例_对python_discover方法遍历所有执行的用例详解
  6. MongoDB 聚合
  7. 使用ADO.NET的参数集合来有效防止SQL注入漏洞
  8. 两个队列实现一个栈思路c语言,两个栈实现队列功能C语言实现能运行!
  9. python 绘制围棋棋盘_奇思妙想(2)五子棋棋盘落子
  10. 人工智能中国专利技术分析报告发布,百度三年蝉联榜首
  11. HTML实现页面注册
  12. win10装鸿蒙双系统,win10下能装双系统教程
  13. 联想IdeaPad 720S-14IKB Compal CIZVO_S0 LA-E581P Rev 2A笔记本PDF点位图
  14. PaddlePaddle入门实践——手写数字识别
  15. 等差、等比数列的求和公式
  16. 来了,来了,他来了,使用Github制作自己的在线简历(网页和PDF版)你都值得拥有
  17. 使用太乐地图下载器下载cesium适用瓦片
  18. Kubeadm安装高可用的K8S集群--多master单node
  19. 二、进程管理(4.经典进程同步问题)
  20. 2017 ACM-ICPC 亚洲区(西安赛区)网络赛 B Coin(逆元,费马小定理)

热门文章

  1. php json设置编码,php实现json编码的方法,phpjson编码
  2. (0026)iOS 开发之模块化封装初步实践
  3. halcon可以用python吗_如何基于pythonnet调用halcon脚本
  4. python安装docx库_linux 环境下的python 安装 docx 的过程
  5. 大北农集团被指控授意窃取商业秘密
  6. angular5 清除定时器
  7. 二叉树原理和作用,总结
  8. 比较文件内容是否相同
  9. tornado 学习笔记15 _ServerRequestAdapter分析
  10. 防止用户利用PHP代码DOS造成用光网络带宽