局域网扫描工具:知道哪些ip地址在使用,哪些没有使用
#!/bin/bash
> up.txt
>down.txt
for i in {1..10}
do
if ping -c 1 -w 1 192.168.0.$i &>/dev/null
then
echo "192.168.0.$i up"
echo "192.168.0.$i" >>up.txt
else
echo "192.168.0.$i down"
echo "192.168.0.$i" >>down.txt
fi
done
echo 可以使用的有
cat up.txt
echo "****************"
echo 不可以使用的有
cat down.txt
串行:一个一个的执行,有顺序的执行的
并行:同时执行,不分顺序,shell里的多进程
改进:
#!/bin/bash
> up.txt
>down.txt
for i in {1..10}
do
(if ping -c 1 -w 1 192.168.0.$i &>/dev/null
then
echo "192.168.0.$i up"
echo "192.168.0.$i" >>up.txt
else
echo "192.168.0.$i down"
echo "192.168.0.$i" >>down.txt
fi)&
done
echo 可以使用的有
cat up.txt
echo "****************"
echo 不可以使用的有
cat down.txt
(命令)&:表示启动一个进程去执行命令,并且将这个命令放到后台执行
多进程产生顺序不同的原因,多个子bash产生,然后进入队列中,有先后顺序,能够ping通的,产生的顺序不一样,ping不通的,阻塞那里又会进行排列
监控类:内存的使用率,超过70%给与提醒
[root@kafka3 lianxi]# cat monitor.sh
#!/bin/bash
#总的内存大小
total=$(free -m|head -2|tail -1|awk '{print $2}')
#剩余的内存大小
free=$(free -m|head -2|tail -1|awk '{print $4}')
#剩余的百分比
num=$(echo "scale=2;$free * 100 / $total " | bc)
#进行结果比较
result=$(echo "$num >= 30"|bc)
if (( $result == 1 ))
then
echo "内存还够用,使用率没有超过70%"
else
echo "内存不够用了,请去清理你的磁盘"
fi
#bc是可以用来小数的运算和小数的比较
抽奖程序:从班级名单里随机抽取学生名字
#!/bin/bash
#清空中奖文件
>person.txt
#产生文件里相应人数之间的随机数
num=$(cat name.txt|wc -l)
while true
do
point=$((RANDOM % num + 1 ))
#读取namt.txt里的文件,中奖的人数
person=$(cat name.txt |head -n $point|tail -1)
if ! grep $person person.txt
then
echo "$person 中奖了"
#person.txt是已经中过奖的名单
echo $person >> person.txt
fi
read -p "请输入任意键继续,按q退出" option
if [[ $option == "q" || $option == "Q" ]]
then
break
fi
echo "中过奖的人的名单:"
echo "------------------"
cat person.txt
echo "------------------"
#判断是否所有人都中奖了
num2=$(cat person.txt |wc -l)
if (( $num == $num2 ))
then "所有人都中奖了"
break
fi
done