当前位置:首页 > linux基础 > 正文内容

Shell介绍(二)函数,数组

6年前 (2019-09-08)linux基础753
函数
命令的集合 完成特定功能的代码块
模块化 复用
函数和变量类似 只有先定义才可以调用,如果只定义不调用 则不会执行
函数的定义和调用
三种方法:
[root@web scripts]# cat fun.sh
#!/bin/sh
test1(){
    echo "第一种函数定义方式"
}
function test2(){
    echo "第二种函数定义方式"
}
function test3 {
    echo "第三种函数定义方式"
}
test1
test2
test3


函数的传参 不能直接传参


1. 在函数名的后面跟参数

fun(){
    if [ -f $1 ];then
        echo "$1 is exsis"
    else
        echo "$1 is no ex"
    fi
}
fun $1


2. 全局配置 在函数的最上面 设置变量

[root@web scripts]# cat fun1.sh
#!/bin/sh
num=20
fun(){
for i in `seq $1`
do
    sum=$[num+i]
done
    echo $sum
}
fun $1


3. local 只在函数体内部生效

[root@web scripts]# cat fun1.sh
#!/bin/sh
fun(){
num=20
for i in `seq $1`
do
    sum=$[num+i]
done
    echo $sum
}
fun $1
数组
普通数组  只能以数字作为索引(下标)
关联数组  可以使用数字也可以使用字符串作为索引(下标)
数组名[索引]=值


定义普通数组


第一种定义方式

数组名[索引]=值
[root@web scripts]# array[0]=shell
[root@web scripts]# array[1]=Linux
[root@web scripts]# array[2]=MySQL
第二种定义方式 一次定义多个值
数组名=(值)
[root@web02 ~]# array=(shell mysql [20]=kvm [50]=test)
[root@web02 ~]# echo ${array[*]}
shell mysql kvm test
[root@web02 ~]# echo ${!array[*]}
0 1 20 50


如何查看值 查看某个索引的值

[root@web scripts]# echo ${array[2]}
MySQL
[root@web scripts]# echo ${array[1]}
Linux
[root@web scripts]# echo ${array[0]}
shell


查看所有的值

[root@web scripts]# echo ${array[*]}
shell Linux MySQL
[root@web scripts]# echo ${array[@]}
shell Linux MySQL


如何查看索引

[root@web scripts]# echo ${!array[*]}
0 1 2
[root@web scripts]# echo ${!arraytest[*]}
0 1 2 3 4
[root@web scripts]# echo ${arraytest[*]}
shell Linux Mysql kvm docker


案例: ping数组内的ip

[root@web scripts]# cat array.sh
#!/bin/sh
ip=(
10.0.0.7
10.0.0.8
10.0.0.254
10.0.0.1
)

for i in ${!ip[*]}
do
    ping -c 1 -W 1 ${ip[$i]}
done


普通数组

[root@web02 ~]# array[index1]=Shell
[root@web02 ~]# array[index2]=Linux
[root@web02 ~]# array[index3]=MySQL
[root@web02 ~]# echo ${array[*]}
MySQL
[root@web02 ~]# echo ${array[1]}

[root@web02 ~]# echo ${array[2]}

[root@web02 ~]# echo ${array[3]}

[root@web02 ~]# echo ${array[0]}
MySQL


如何定义关联数组

declare -A array
[root@web02 ~]# declare -A array
[root@web02 ~]# array[index1]=Shell
[root@web02 ~]# array[index2]=Linux
[root@web02 ~]# array[index3]=MySQL
[root@web02 ~]# echo ${array[index1]}
Shell
[root@web02 ~]# echo ${array[index2]}
Linux
[root@web02 ~]# echo ${array[index3]}
MySQL
[root@web02 ~]# echo ${array[*]}
Shell Linux MySQL
[root@web02 ~]# echo ${!array[*]}
index1 index2 index3


查看数组的长度

echo ${#array[*]}


遍历数组 三种方式

1. echo ${array[*]}   for循环
2. echo ${!array[*]}  使用索引遍历内容
3. echo ${#array[*]}  索引的个数遍历内容
[root@web02 ~]# let array[a]++
[root@web02 ~]# let array[a]++
[root@web02 ~]# let array[a]++
[root@web02 ~]# let array[a]++
[root@web02 ~]# let a++
[root@web02 ~]# let a++
[root@web02 ~]# echo $a
2
[root@web02 ~]# let b++
[root@web02 ~]# let b++
[root@web02 ~]# echo $b
2
[root@web02 ~]# let array[b]++
[root@web02 ~]# let array[b]++
[root@web02 ~]# let array[b]++
[root@web02 ~]# echo ${array[b]}
[root@web02 ~]# cat array.sh
#!/bin/sh
declare -A array
for i in `cat sex.txt`
do
    let array[$i]++
done
for i in ${!array[*]}
do
    echo "$i出现了 ${array[$i]}次"
done
案例2:
[root@web02 ~]# cat sex.txt
zs m
ls m
em f
alex m
ld m
oldboy f
bgx x
[root@web02 ~]# cat array.sh 
#!/bin/sh
declare -A array
while read line
do
    type=`echo $line|awk '{print $2}'`
    let array[$type]++
done<sex.txt
for i in ${!array[*]}
do
    echo "$i出现了 ${array[$i]}次"
done


案例3

[root@web02 ~]# cat array.sh 
#!/bin/sh
declare -A array
while read line
do
    let array[`echo $line|awk '{print $2}'`]++
done<sex.txt
for i in ${!array[*]}
do
    echo "$i出现了 ${array[$i]}次"
done

统计IP地址
[root@web02 ~]# cat array.sh
#!/bin/sh
declare -A array
while read line
do
    type=`echo $line|awk '{print $1}'`
    let array[$type]++
done</var/log/nginx/access.log
for i in ${!array[*]}
do
    echo "$i出现了 ${array[$i]}次"
done


“Shell介绍(二)函数,数组” 的相关文章

rpm包管理

rpm包管理

RPM基础概述RPM全称RPM Package Manager缩写,由红帽开发用于软件包的安装,升级卸载与查询rpm包的命名规则一个rpm软件包的命名规则。bash-4.2.46-28.el7.x86_64.rpmname: 软件包名称version: 版本号, 主版本, 重大更新. 次版本, 子...

Shell介绍(三)if判断,case控制,for循环,while循环,流程控制语句

if判断单分支if [你有房] then     我就嫁给你 fi多分支if [ 你有房 ] then     我就嫁给你 else   &nbs...

自动化运维(一)Ansible安装及模块的使用

自动化运维(一)Ansible安装及模块的使用

1:Ansible安装1.什么是AnsibleAnsible是python 中的一套模块,系统中的一套自动化工具,只需要使用ssh协议连接及可用来系统管理、自动化执行命令等任务。2.Ansible优势1、ansible不需要单独安装客户端,也不需要启动任何服务2、ansible是python中的一套...

自动化运维(三)Ansible Playbook案例

自动化运维(三)Ansible Playbook案例

Playbook案例1.环境规划角色外网IP(NAT)内网IP(LAN)部署软件m01eth0:10.0.0.61eth1:172.16.1.61ansiblebackupeth0:10.0.0.41eth1:172.16.1.41rsyncnfseth0:10.0.0.31eth1:172.16....

linux 查看系统信息命令

# uname -a # 查看内核/操作系统/CPU信息 # head -n 1 /etc/issue # 查看操作系统版本 # cat /proc/cpuinfo # 查看CPU信息 # hostname # 查看计算机名 # lspci -tv # 列出所...