Compare commits

...

3 Commits

Author SHA1 Message Date
9f86a88401
feat: while和util循环 2024-07-15 14:46:11 +08:00
6a12113be3
feat: 扫描子网可达主机脚本 2024-07-15 14:45:23 +08:00
a4d375af41
feat: 添加奇偶判断函数 2024-07-14 14:59:43 +08:00
5 changed files with 85 additions and 0 deletions

View File

@ -0,0 +1,25 @@
#!/bin/bash
#Program:
# 扫描子网可达的主机
#History:
# 2024/07/14 LouisFonda<yigencong@yahoo.com>
subnet="192.168.1"
reachable_hosts=()
# 循环遍历子网中的所有IP地址
for (( i=1; i<=255; i++ ))
do
ip="$subnet.$i"
# 发送单个ICMP回显请求等待1秒钟最多发送1次
ping -c 1 -W 1 "$ip" > /dev/null 2>&1
# 检查ping命令的退出状态
if [ $? -eq 0 ]; then
echo "$ip 可达"
reachable_hosts+=("$ip")
fi
done
echo "可达主机列表:"
echo "${reachable_hosts[@]}"

View File

@ -0,0 +1,22 @@
#Program:
# 编写一个函数判断一个数的奇偶性
#History:
# 2024/07/14 LouisFonda<yigencong@yahoo.com>
# 函数is_even
is_even () {
test $(expr $1 % 2) -eq 0 && return 0 || return 1
}
read -p "请输入一个整数:" num
# 判断这个数是否合法
if [ "$num" != "" ] && echo "$num" | grep -qE "^-?[0-9]+";then
if is_even $num;then
echo "这个数是偶数"
else
echo "这个数是奇数"
fi
else
echo "输入的数值不和法,请输入整数"
fi

View File

@ -0,0 +1,10 @@
#!/bin/bash
#Program:
# 判断输入如果用户输入yes结束循环否则继续
#History:
# 2024/07/14 LouisFonda<yigencong@yahoo.com>
until [ "${yn}" == "yes" -o "${yn}" == "YES" ]
do
read -p "是否结束运行" yn
done

View File

@ -0,0 +1,11 @@
#!/bin/bash
#Program:
# 判断输入如果用户输入yes就继续执行否则结束运行
#History:
# 2024/07/14 LouisFonda<yigencong@yahoo.com>
yn="yes"
while [ "${yn}" == "yes" -o "${yn}" == "YES" ]
do
read -p "是否继续运行" yn
done

View File

@ -0,0 +1,17 @@
#!/bin/bash
#Program:
# 输入一个值计算从0到这个值的所有和比如输入100那么输出5050
num=0
sum=0
if [ -n "$1" ] && echo "$1" | grep -Eq "^[-+]?[0-9]+$" && [ "$1" -ge 0 ];then
while [ "$num" -le "$1" ]
do
sum=$(($num+$sum))
num=$((num+1))
done
echo "1-${1}求和的结果为:${sum}"
else
echo "输入的数值不正确必须为非空且大与等于0的整数"
fi