shell脚本进阶

一、探测同网段哪些ip正在被使用

[root@rocky8 ~]#cat ping.sh 
#!/bin/bash
#**********************************************************
#Author:                    liangweisong
#QQ:                        965291014@qq.com
#Date:                      2024-08-31
#FileName:                  ping.sh
#URL:                       
#Description:                   The test script
#**********************************************************
NET=10.0.0
#NET为所处网段的前24位十进制地址
for i in  {1..254};do
{
 ping  -c1  -W1 $NET.$i &> /dev/null && echo $NET.$i is up| tee -a ./up_list.txt || echo $NET.$i is down
}&
done
wait
#创建文件
[root@rocky8 ~]#touch up_list.txt
#执行脚本
[root@rocky8 ~]#bash ping.sh
10.0.0.1 is up
10.0.0.2 is up
10.0.0.210 is up
10.0.0.153 is up
10.0.0.3 is down
......
[root@rocky8 ~]#cat up_list.txt
10.0.0.1 is up
10.0.0.2 is up
10.0.0.210 is up
10.0.0.153 is up

二、使用while read line和/etc/passwd,计算用户id总和。

while 循环的特殊用法,遍历文件或文本的每一行

while read line; do
循环体
done < /PATH/FROM/SOMEFILE
[root@rocky8 ~]#cat wrl.sh
#!/bin/bash
#**********************************************************
#Author:                    liangweisong
#QQ:                        965291014@qq.com
#Date:                      2024-08-31
#FileName:                  wrl.sh
#URL:                       
#Description: 使用while read line和/etc/passwd,计算用户id总和。
#**********************************************************
# 初始化 UID 总和为 0
uid_sum=0

# 逐行读取 /etc/passwd 文件
while IFS=: read -r username password uid gid fullname home_directory shell; do
    # 将 UID 转换为整数并累加到总和中
    uid_sum=$((uid_sum + uid))
done < /etc/passwd

# 打印 UID 总和
echo "The sum of all user IDs is: $uid_sum"
[root@rocky8 ~]#bash wrl.sh
The sum of all user IDs is: 72071

三、总结索引数组和关联数组,字符串处理,高级变量使用及示例。

3.1 索引数组

索引数组是一种基于数字索引的数组。每个元素都有一个唯一的整数索引。

创建索引数组

# 创建一个空数组
indexed_array=()

# 添加元素
indexed_array+=(element1 element2 element3)

# 或者
indexed_array[0]="element1"
indexed_array[1]="element2"
indexed_array[2]="element3"

访问数组元素

# 访问第一个元素
first_element=${indexed_array[0]}

# 访问最后一个元素
last_element=${indexed_array[-1]}

# 遍历数组
for element in "${indexed_array[@]}"; do
    echo "$element"
done

3.2 关联数组

关联数组是一种基于键值对的数组。每个元素都有一个唯一的键(字符串)和对应的值。

创建关联数组

# 创建一个空关联数组
declare -A assoc_array

# 添加元素
assoc_array[key1]="value1"
assoc_array[key2]="value2"
assoc_array[key3]="value3"

访问关联数组元素

# 访问键为 key1 的元素
value1=${assoc_array[key1]}

# 遍历关联数组
for key in "${!assoc_array[@]}"; do
    value=${assoc_array[$key]}
    echo "$key: $value"
done

1.3 字符串处理

字符串拼接

string1="Hello"
string2="World"
full_string="$string1 $string2"

字符串替换

string="Hello World"
new_string="${string//World/Universe}"
echo "$new_string"  # 输出 "Hello Universe"

字符串分割

string="apple,banana,orange"
IFS=',' read -ra fruits <<< "$string"
for fruit in "${fruits[@]}"; do
    echo "$fruit"
done

字符串长度

string="Hello World"
length=${#string}
echo "$length"  # 输出 11

四、求10个随机数的最大值与最小值。

[root@rocky8 ~]#cat random.sh 
#!/bin/bash
#**********************************************************
#Author:                    liangweisong
#QQ:                        965291014@qq.com
#Date:                      2024-08-31
#FileName:                  random.sh
#URL:                       
#Description: 求10个随机数的最大值与最小值。
#**********************************************************
# 初始化最大值和最小值变量
max=-999999
min=999999

# 循环10次来生成10个随机数
for i in {1..10}
do
    # 生成1到100之间的随机数
    num=$(( (RANDOM % 100) + 1 ))

    # 打印生成的随机数
    echo "随机数 $i: $num"

    # 更新最大值和最小值
    if [ "$num" -gt "$max" ]; then
        max="$num"
    fi
    if [ "$num" -lt "$min" ]; then
        min="$num"
    fi
done

# 输出最大值和最小值
echo "最大值: $max"
echo "最小值: $min"
[root@rocky8 ~]#bash random.sh 
随机数 1: 87
随机数 2: 7
随机数 3: 100
随机数 4: 48
随机数 5: 12
随机数 6: 17
随机数 7: 2
随机数 8: 98
随机数 9: 50
随机数 10: 33
最大值: 100
最小值: 2

五、使用递归调用,完成阶乘算法实现。

[root@rocky8 ~]#cat factorial.sh
#!/bin/bash
#**********************************************************
#Author:                    liangweisong
#QQ:                        965291014@qq.com
#Date:                      2024-08-31
#FileName:                  test.sh
#URL:                       
#Description:                   The test script
#**********************************************************
# 阶乘函数
factorial() {
    local n=$1
    # 基本情况
    if [ "$n" -eq 0 ]; then
        echo 1
    else
        # 递归调用
        echo $(( n * $(factorial $((n - 1))) ))
    fi
}

# 测试阶乘函数
number=9
result=$(factorial $number)
echo "$number 的阶乘是 $result"

此条目发表在第四周作业分类目录。将固定链接加入收藏夹。

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注