shell 之 数组

函数传参使用场景示例,需求描述:写一个脚本,实现nginx服务的启动、停止、重启。

[root@shell day06]# cat nginx.sh
#!/bin/bash
source  /etc/init.d/functions
if [ $# -ne 1 ];then
    echo "Usage: $0 {start|stop|status|reload|restart}"
    exit
fi
rc=$1
retu() {
    if [ $? -eq 0 ];then
        action "Nginx is $rc 成功!"  /bin/true
    else
        action "Nginx is $rc 失败!"  /bin/false
    fi
}
start() {
    if [ ! -f /var/run/nginx.pid ];then
        /usr/sbin/nginx
        retu
    else
        echo "Nginx 服务正在运行....."
    fi
}
stop() {
    if [  -f /var/run/nginx.pid ];then
        /usr/sbin/nginx -s stop
        retu
    else
        echo "Nginx 服务不在运行......"
    fi
}
reload() {
    if [ -f /var/run/nginx.pid ];then
        /usr/sbin/nginx -s reload 
        retu
    else
        echo "Nginx 服务不在运行,无法进行重启!"
    fi
}
status() {
    if [ -f /var/run/nginx.pid ];then
        echo "Nginx 服务正在运行....."
    else
        echo ""Nginx 服务不在运行.....
    fi
}
case $rc in 
    start)
            start
            ;;
    stop)
            stop
            ;;
    reload)
            reload
            ;;
    restart)
            rc=stop
            stop
            sleep 2
            rc=start
            start
            ;;
    status)
            status
            ;;
    *)
            echo "Usage: $0 {start|stop|status|reload|restart}"
            exit
esac

echo 数据

return 返回状态码 0-255 正整数 0 成功 非0 失败

[root@shell day06]# cat state.sh
#!/bin/bash
fun() {
echo 100
return 1
}
tt=$(fun)
echo 当前返回的数据为$tt ,状态码为 $? 

[root@shell day06]# cat state-1.sh
#!/bin/bash
fun(){
if [  -f /etc/hosts ];then
    return 1
else
    return 2
fi
}
fun && echo "文件存在" || echo "文件不存在"
Shell函数返回值场景练习,例:猜数字游戏。
1.如果用户输入的数值大于0且小于10则返回0
2.如果用户输入的数值大于等于10且小于20则返回1
3.如果用户输入的数值大于等于20且小于30则返回2
4.输入其余数值则返回3 
[root@shell day06]# cat state-2.sh
#!/bin/bash
return_num () {
    read -p "请输入一个数字:" num
    if [ $num -gt 0 -a $num -lt 10 ];then
        return 0
    elif [ $num -ge 10 -a $num -lt 20 ];then
        return 1
    elif [ $num -ge 20 -a $num -lt 30 ];then
        return 2
    else
        return 3
    fi
}
return_num
rc=$?
case $rc in
    0)
        echo "你输入的数字大于0且小于10。"
        ;;
    1)
        echo "你输入的数字大于等于10且小于20。"
        ;;
    2)
        echo "你输入的数字大于等于20且小于30。"
        ;;
    3)
        echo "你输入的数字不认识。"
esac

作业

  1. 系统初始化版本
3. 编写系统管理工具箱

查看内存的使用情况
查看磁盘的使用情况
查看系统的负载
等等
q键退出程序

[root@qiudao /scripts]# cat case-6.sh
#!/bin/bash
menu() {
cat <<-EOF
#############################

## h 命令帮助

## d 磁盘使用情况

## m 内存使用情况

## v 系统版本

## k 系统内核版本

## n 系统静态主机名

## e eth0网卡ip地址

## i 外网ip地址

## u 系统负载情况

## q 退出程序

#############################
EOF
}
menu
while true
do
read -p "根据上方的菜单,选择你要查看的信息: " sys
case $sys in
    h)
        clear
        menu
        ;;        
    d)
        clear
        df -h
        ;;
    m)
        clear
        free -m
        ;;
    v)
        clear
        awk '{print $1,$2,$4}' /etc/redhat-release
        ;;
    k)
        clear
        uname -r
        ;;
    n)
        clear
        hostnamectl|grep "Static hostname"|awk '{print $3}'
        ;;
    e)
        clear
        ifconfig eth0|awk 'NR==2{print $2}'
        ;;
    i)
        clear
        curl -s icanhazip.com
        ;;
    q)
        clear
        exit
        ;;
    *)
        clear
        echo "USAGE: $0 [ h | d | m | v | k | n | e | i | q ]"
esac
done
数组

普通数组		只能用数字作为索引,不需要指定

关联数组		可以使用字符作为索引,需要指定
普通数组定义
[root@shell day06]# array1[0]=jinke
[root@shell day06]# array1[1]=zhangsong
[root@shell day06]# array1[2]=shaobai
[root@shell day06]# array2=(linux python mysql)
[root@shell day06]# array3=($(cat /etc/hosts))
#查看普通数组的值

#查看数组的索引个数
[root@shell day06]# echo ${#array1[@]}
3
#查看所有索引的名称
[root@shell day06]# echo ${!array1[@]}
0 1 2

#查看数组的所有值
[root@shell day06]# echo ${array1[@]}
jinke zhangsong shaobai

#查看0索引对应的值
[root@shell day06]# echo ${array1[0]}
jinke
[root@shell day06]# echo ${array1[1]}
zhangsong

#从第一列开始显示1列值,默认是从0列开始计算
[root@shell day06]# echo ${array1[@]:1:1}
zhangsong
[root@shell day06]# echo ${array1[@]:0:1}
jinke
[root@shell day06]# echo ${#array2[*]}
3
[root@shell day06]# echo ${!array2[*]}
0 1 2
[root@shell day06]# echo ${array2[*]}
linux python mysql
[root@shell day06]# echo ${array2[1]}
python
[root@shell day06]# echo ${array2[0]}
linux

#查看所有的普通数组
[root@shell day06]# declare

[root@shell day06]# echo ${!array3[*]}
0 1 2 3 4 5 6 7 8 9
[root@shell day06]# echo ${array3[0]}
127.0.0.1
[root@shell day06]# echo ${array3[9]}
localhost6.localdomain6


关联数组

#指定该数组为关联数组
[root@shell day06]# declare  -A  guanlian

#定义关联数组
[root@shell day06]# guanlian=([name]=jinke [age]=18 [skill]=yangzhu)

[root@shell day06]# echo ${#guanlian[@]}
3
[root@shell day06]# echo ${!guanlian[@]}
name age skill
[root@shell day06]# echo ${guanlian[@]}
jinke 18 yangzhu
[root@shell day06]# echo ${guanlian[name]}
jinke
[root@shell day06]# echo ${guanlian[age]}
18
[root@shell day06]# echo ${guanlian[skill]}
yangzhu

#普通数组
[root@shell day06]# putong=([3]=zhangsong [5]=shell)
[root@shell day06]# echo ${!putong[@]}
3 5
[root@shell day06]# echo ${putong[@]}
zhangsong shell
[root@shell day06]# echo ${putong[5]}
shell


数组遍历与循环

方式二:通过数组元数的索引进行遍历(推荐) 注意: 将统计的对象作为数组的索引,仅针对关联数据。



01.普通数组赋值与遍历示例

[root@shell day06]# cat array-1.sh
#!/bin/bash
while read line
do
    host_array[i++]=$line
done </etc/hosts

for i in ${!host_array[@]}
do
    echo "host_array 数组的索引:$i ,对应的值为:${host_array[$i]}"
done



[root@shell day06]# sh  array-1.sh
host_array 数组的索引:0 ,对应的值为:127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
host_array 数组的索引:1 ,对应的值为:::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

[root@shell day06]# cat array-1.sh
#!/bin/bash
while read line
do
    host_array[i++]=$line
    a=$(($i-1))
    echo $a  ${host_array[$a]}
done </etc/hosts



02.使用关联数组统计文件中的每个Shell数量



[root@shell day06]# cat array-2.sh
#!/bin/bash
declare -A shell_num 
while read line
do
    Shell=$(echo $line |awk -F: '{print $NF}')
    let  shell_num[$Shell]++
done </etc/passwd
for j in ${!shell_num[@]}
do
    echo "当前shell为:$j ,存在的数量为:${shell_num[$j]}"
done


  1. 统计Nginx日志IP访问次数
[root@git day6]# cat array4.sh 
#!/bin/bash
declare -A shell_num
while read line
do
    shell=$(echo $line |awk '{ print $1 }')

   let shell_num[$shell]++
    
done < driverzeng.com_access.log

for j in ${!shell_num[@]}

do
echo "当前shell $j, 存在的数量为 ${shell_num[$j]}"
done


  1. 统计tcp的状态信息

    [root@git day6]# cat tcp.sh 
    #!/bin/bash
    declare -A shell_num
    shell=$(netstat -ant|awk 'NR>2 {print $NF}')
    for i in $shell
    do
       let shell_num[$i]++
      done  
    for j in ${!shell_num[@]}
    
    do
    echo "当前shell $j, 存在的数量为 ${shell_num[$j]}"
    done
    
    
posted on 2019-10-23 21:26  酷gril  阅读(333)  评论(0编辑  收藏  举报