shell中的函数、shell的数组、告警系统需求分析
发布时间:2020-12-15 19:56:26 所属栏目:安全 来源:网络整理
导读:shell中的函数 函数就是把一段代码整理到了一个小单元中,并给这个小单元起一个名字,当用到这段代码时直接调用这个小单元的名字即可。 格式: function f_name() { command }函数必须要放在最前面 脚本示例1(用来定义函数打印参数) [root@garytao-01 aming]
shell中的函数
格式: function f_name() { command } 函数必须要放在最前面
[root@garytao-01 aming]# vim fun1.sh [root@garytao-01 aming]# cat fun1.sh #!/bin/bash function inp(){ echo $1 $2 $3 $0 $# } inp 1 a 2 [root@garytao-01 aming]# sh fun1.sh 1 a 2 fun1.sh 3 [root@garytao-01 aming]# sh -x fun1.sh + inp 1 a 2 + echo 1 a 2 fun1.sh 3 1 a 2 fun1.sh 3 [root@garytao-01 aming]# vi fun1.sh [root@garytao-01 aming]# cat fun1.sh #!/bin/bash function inp(){ echo "The first par is $1" echo "The second par is $2" echo "The third par is $3" echo "the scritp name is $0" echo "the number of par is $#" } inp b a 2 3 adf [root@garytao-01 aming]# sh fun1.sh The first par is b The second par is a The third par is 2 the scritp name is fun1.sh the number of par is 5 [root@garytao-01 aming]# sh -x fun1.sh + inp b a 2 3 adf + echo 'The first par is b' The first par is b + echo 'The second par is a' The second par is a + echo 'The third par is 2' The third par is 2 + echo 'the scritp name is fun1.sh' the scritp name is fun1.sh + echo 'the number of par is 5' the number of par is 5 [root@garytao-01 aming]#
[root@garytao-01 aming]# vim fun2.sh [root@garytao-01 aming]# cat fun2.sh #!/bin/bash sum() { s=$[$1+$2] echo $s } sum 1 2 [root@garytao-01 aming]# sh fun2.sh 3 [root@garytao-01 aming]# sh -x fun2.sh + sum 1 2 + s=3 + echo 3 3 [root@garytao-01 aming]#
[root@garytao-01 aming]# vim fun3.sh [root@garytao-01 aming]# cat fun3.sh #!/bin/bash ip() { ifconfig |grep -A1 "$1: "|awk '/inet/ {print $2}' } read -p "Please input the eth name: " eth ip $eth [root@garytao-01 aming]# sh fun3.sh Please input the eth name: ens33 172.16.111.100 [root@garytao-01 aming]# sh -x fun3.sh + read -p 'Please input the eth name: ' eth Please input the eth name: ens33 + ip ens33 + grep -A1 'ens33: ' + awk '/inet/ {print $2}' + ifconfig 172.16.111.100 shell中的数组1.shell中的数组1
[root@garytao-01 aming]# b=(1 2 3) [root@garytao-01 aming]# echo ${b[@]} 1 2 3 [root@garytao-01 aming]# echo ${b[*]} 1 2 3 [root@garytao-01 aming]# echo ${b[1]} 2 [root@garytao-01 aming]# echo ${b[2]} 3 [root@garytao-01 aming]# echo ${b[0]} 1 [root@garytao-01 aming]# echo ${#b[@]} 3 [root@garytao-01 aming]# b[3]=a [root@garytao-01 aming]# echo ${b[*]} 1 2 3 a [root@garytao-01 aming]# b[3]=aaa [root@garytao-01 aming]# echo ${b[*]} 1 2 3 aaa [root@garytao-01 aming]# unset b[3] //删除数组 [root@garytao-01 aming]# echo ${b[*]} 1 2 3 [root@garytao-01 aming]# unset b [root@garytao-01 aming]# echo ${b[*]} [root@garytao-01 aming]# 2.shell中的数组2
[root@garytao-01 aming]# a=(`seq 1 10`) [root@garytao-01 aming]# echo ${a[*]} 1 2 3 4 5 6 7 8 9 10 [root@garytao-01 aming]# echo ${a[@]:3:4} 4 5 6 7 [root@garytao-01 aming]# echo ${a[@]:0-3:2} 8 9 [root@garytao-01 aming]# echo ${a[@]/8/6} 1 2 3 4 5 6 7 6 9 10 [root@garytao-01 aming]# a=(${a[@]/8/6}) [root@garytao-01 aming]# echo ${a[@]} 1 2 3 4 5 6 7 6 9 10 [root@garytao-01 aming]# 告警系统需求分析1.shell项目-告警系统
bin下是主程序;conf下是配置文件;shares下是各个监控脚本;mail下是邮件引擎;log下是日志。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
推荐文章
站长推荐
热点阅读