练习_for循环体后台执行

#!/usr/bin/bash

>ip.txt #重定向 清理之前文件内容

for i in {2..254}
do
        {
                ip=192.168.201.$i
                ping -c1 -W1 $ip &>/dev/null
                if [ $? -eq 0 ];then
                        echo "$ip" | tee -a ip.txt
                fi
        }&  #放入后台执行 会在子shell中进行
done

wait #等待后台任务执行完成
echo "finish...."

 

posted @ 2022-11-30 15:11  smatterer  阅读(87)  评论(0)    收藏  举报