#!/bin/bash declare -a hosttmpfiles my_trap() { echo "##############" rm -f ${hosttmpfiles[@]} exit 1 } trap 'my_trap' INT for i in {1..100}; do tmpfile=$(mktemp /tmp/ping.XXXXXX) if ping -w 1 -c 1 192.168.0.$i &>/dev/null; then echo "192.168.0.$i is up" | tee $tmpfile else echo "192.168.0.$i is down" | tee $tmpfile fi hosttmpfiles[${#hosttmpfiles[*]}]=$tmpfile done rm -f ${hosttmpfiles[@]}