以下脚本将pid(进程ID)间隔(以秒为单位)和持续时间(以分钟为单位)作为输入。我想只在output.txt中添加数据并删除重新数据,但这不会发生。请帮忙
#!/bin/bash
pid=$1
interval=$2
duration=$3
#to get current time
now="$(date +"%r")"
echo $now
# to set duration in seconds
let "duration=$duration * 60"
end=$((SECONDS+duration))
if [ $( ls ~/output.txt ) ]
then
rm -f ~/output.txt
fi
while [ $SECONDS -lt $end ]
do
echo "$(pidstat -h -p $pid -d -r -u -w)"
output="$(pidstat -h -p $pid -d -r -u -w)"
echo "$(pidstat -h -p $pid -d -r -u -w)" >> ~/output.txt
sleep $interval
done
lines="$(wc -l < ~/output.txt)"
echo $lines
c=1
while [ $c -le $lines ]
do
if (( $c % 4 != 0 ))
then
sed -i "${c}d" ~/output.txt
fi
c=`expr $c + 1`
done
liness="$(wc -l < ~/output.txt)"
echo $liness
echo duration:$duration interval:$interval processId:$pid
答案 0 :(得分:0)
迭代文件行并在每行上调用sed -i
不是一个好方法。如果要删除〜/ output.txt的每4行,只需执行:
tmp=$(mktemp)
awk 'NR%4' ~/output.txt > $tmp; mv $tmp ~/output.txt