项目四实训 Shell数组与函数

项目四实训 Shell数组与函数

任务一 编写Shell数组脚本

[root@ly ~]# vim osversion.txt
[root@ly ~]# cat osversion.txt
Openeuler,linux
RHEL9,linux
Ubuntu22,linux
Debian,linux
Solaris,unix
IBM-AIX,unix
HP-UX,unix
[root@ly ~]# vim array-count.sh
[root@ly ~]# cat array-count.sh
#!/bin/bash
# 声明一个关联数组以存储操作系统统计数
declare -A os_count
# 逐行读取文件
while IFS=',' read -r name os
do
    # 增加当前操作系数计数
    ((os_count[$os]++))
done < osversion.txt
# 输出操作系统计数
echo "Operate System Count:"
for os in "${!os_count[@]}"
do
    echo "$os: ${os_count[$os]}"
done
[root@ly ~]# bash array-count.sh
Operate System Count:
linux: 4
unix: 3
[root@ly ~]#

(2)在家目录中,创建脚本文件array-shelltype.sh

[root@ly ~]# vim array-shelltype.sh
[root@ly ~]# cat array-shelltype.sh
#!/bin/bash
declare -A shells
while IFS=':' read -r user _ _ _ _ _ shell _; do
    shells[$shell]=$((shells[$shell] + 1))
done < /etc/passwd
for shell in "${!shells[@]}"; do
    echo "Shell $shell is used by ${shells[$shell]} users"
done
[root@ly ~]# bash array-shelltype.sh
Shell /sbin/nologin is used by 21 users
Shell /bin/bash is used by 13 users
Shell /bin/sync is used by 1 users
Shell /usr/sbin/nologin is used by 1 users
Shell /sbin/halt is used by 1 users
Shell /sbin/shutdown is used by 1 users
[root@ly ~]# 

(3)在家目录中,创建脚本文件array-stat.sh

[root@ly ~]# vim array-stat.sh
[root@ly ~]# cat array-stat.sh
#!/bin/bash
declare -A connections
while read line; do
    state=$(echo $line | awk '{print $2}')
    if [ -z "${connections[$state]}" ]; then
        connections[$state]=1
    else
        connections[$state]=$((connections[$state] + 1))
    fi
done < <(ss -an | awk '{print $1,$2}')
for state in "${!connections[@]}"; do
    echo "State $state: ${connections[$state]}"
done
[root@ly ~]# bash array-stat.sh
State ESTAB: 83
State UNCONN: 48
State LISTEN: 22
State State: 1
[root@ly ~]# 

任务二 编写Shell函数脚本

(1)在用户家目录中,创建脚本文件func-fibonacci.sh

[root@ly ~]# vim func-fibonacci.sh
[root@ly ~]# cat func-fibonacci.sh
#!/bin/bash
# 定义函数来生成斐波那契数列
generate_fibonacci() {
    local n=$1
    local fib_sequence=()
    # 初始化前两个数
    fib_sequence[0]=0
    fib_sequence[1]=1
    # 计算并存储数列中的下一个元素
    for ((i=2; i<=n; i++))
    do
        fib_sequence[i]=$((fib_sequence[i-1] + fib_sequence[i-2]))
    done
    # 将数列作为返回值
    echo "${fib_sequence[@]}"
}
# 调用函数生成斐波那契数列并输出
fibonacci_sequence=$(generate_fibonacci 20)
echo "Fibonacci Sequence:"
echo $fibonacci_sequence
[root@ly ~]# bash func-fibonacci.sh
Fibonacci Sequence:
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765
[root@ly ~]# 

(2)在家目录中,创建脚本文件func-bubble-sort.sh

[root@ly ~]# vim func-bubble-sort.sh
[root@ly ~]# cat func-bubble-sort.sh
#!/bin/bash
function bubble_sort {
    local arr=("$@")
    local n=${#arr[@]}
    local temp
    for((i=0; i<n-1; i++)); do
        for((j=0; j<n-i-1; j++)); do
            if [[ ${arr[j]} -gt ${arr[$((j+1))]} ]]; then
                temp=${arr[j]}
                arr[$j]=${arr[$((j+1))]}
                arr[$((j+1))]=$temp
            fi
        done
    done
    echo ${arr[@]}
}
arr=(5 3 8 4 1)
sorted_arr=($(bubble_sort ${arr[@]}))
echo "Original array: ${arr[@]}"
echo "Sorted array: ${sorted_arr[@]}"
[root@ly ~]# bash func-bubble-sort.sh
Original array: 5 3 8 4 1
Sorted array: 1 3 4 5 8
[root@ly ~]#

(3)在家目录中,创建脚本文件func-process-check.sh

[root@ly ~]# vim func-process-check.sh
[root@ly ~]# cat func-process-check.sh
#!/bin/bash
# 定义函数,获取并输出CPU使用率排名前5的进程信息
get_top_processes() {
    # 使用 top 命令获取进程信息
    top_output=$(top -b -n 1 -o %CPU)
    # 提取前5个进程信息
    top_processes=$(echo "$top_output" | awk 'NR>7 {print $1, $9, $12}' | head -n 5)
    # 输出前5个进程的PID、CPU使用率和进程正在执行的实际命令
    echo "Top 5 processes by CPU usage:"
    counter=0
    while read -r pid cpu command; do
        cpu=$(echo "$cpu" | awk '{printf "%.1f%%", $1}')
        echo "PID: $pid, CPU Usage: $cpu, Command: $command"
        ((counter++))
        if [[ $counter -eq 5 ]]; then
            break
        fi
    done <<< "$top_processes"
}
# 调用函数
get_top_processes
[root@ly ~]# bash func-process-check.sh
Top 5 processes by CPU usage:
PID: 1406, CPU Usage: 5.6%, Command: top
PID: 1, CPU Usage: 0.0%, Command: systemd
PID: 2, CPU Usage: 0.0%, Command: kthreadd
PID: 3, CPU Usage: 0.0%, Command: rcu_gp
PID: 4, CPU Usage: 0.0%, Command: rcu_par_gp
[root@ly ~]# 

PU Usage: 0.0%, Command: systemd
PID: 2, CPU Usage: 0.0%, Command: kthreadd
PID: 3, CPU Usage: 0.0%, Command: rcu_gp
PID: 4, CPU Usage: 0.0%, Command: rcu_par_gp
[root@ly ~]#


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值