2016-09-21 1 views
0

Я хочу, чтобы мой файл Войти как то:Bash: выполнить команду несколько раз и язычок вывода отделен в файл

Time_Namelookup: 0,1 0,2 0,12 0,45  ... 
Time_Connect: 0,34 0,23 0,23 0,11   ... 
Time_Starttransfer: 0,9 0,23 0,12   ... 

Я хочу, чтобы значения добавляются к их конкретной линии каждых п секунд.

Я получил код, как это:

while [ "true" ] 
do 
echo "Time_Namelookup:" >> $file 
curl -w "%{time_namelookup}\t" -o /dev/null -s https://website.com/ 
echo "Time_connect" >> $file 
curl -w "%{time_connect}\t" -o /dev/null -s https://website.com/ 
echo "Time_Starttransfer:" >> $file 
curl -w "%{time_starttransfer}\t" -o /dev/null -s https://website.com/ 
sleep 5 
done 

Но я получаю что-то вроде

Time_Namelookup: 0,1   
Time_Connect: 0,34   
Time_Starttransfer: 0,9 

Time_Namelookup: 0,2  
Time_Connect:0,23 0,23   
Time_Starttransfer: 0,23 

Time_Namelookup: 0,45  
Time_Connect: 0,11   
Time_Starttransfer: 0,12 

Можете ли вы мне помочь?

ответ

0

Вы можете поместить это внутри петли

if [ ! -f $file ]; then 
    echo "Time_Namelookup:" > $file 
    echo "Time_Connect:" >> $file 
    echo "Time_Starttransfer:" >> $file 
fi 

name_lookup=$(curl -w "%{time_namelookup}\t" -o /dev/null -s https://website.com/) 
connect=$(curl -w "%{time_connect}\t" -o /dev/null -s https://website.com/) 
starttransfer=$(curl -w "%{time_starttransfer}\t" -o /dev/null -s https://website.com/) 
sed -i -e "s/\(Time_Starttransfer:.*\)/\1 $starttransfer/" \ 
    -e "s/\(Time_Connect:.*\)/\1 $connect/" \ 
    -e "s/\(Time_Starttransfer:.*\)/\1 $starttransfer/" \ 
    $file 
+0

Это, видимо, делает то, что ОП хочет, но это абсолютно безобразно. Один' СЕПГ -i -e" 1s/\ $/$ firstvalue/"-e" 2s/\ $/$ secondvalue/"-e" 3s/\ $/$ thirdvalue/"" $ file "' по крайней мере не позволит переписать файл несколько раз за итерацию. – tripleee

0

вы можете попробовать это;

file=yourFile 
echo "Time_Namelookup :" >> $file 
echo "Time_Connect  :" >> $file 
echo "Time_Starttransfer:" >> $file 
while [ "true" ] 
do 
time_namelookup=$(curl -w "%{time_namelookup}\t" -o /dev/null -s https://website.com/) 
time_connect=$(curl -w "%{time_connect}\t" -o /dev/null -s https://website.com/) 
time_starttransfer=$(curl -w "%{time_starttransfer}\t" -o /dev/null -s https://website.com/) 
sed -i "/Time_Namelookup :/s/$/\t$time_namelookup/" $file 
sed -i "/Time_Connect  :/s/$/\t$time_connect/" $file 
sed -i "/Time_Starttransfer:/s/$/\t$time_starttransfer/" $file 
sleep 5 
done 

sed -i : подлежит редактированию на месте.

Следующая часть должна найти «Time_Namelookup:» в файле.

/Time_Namelookup :/ 

/s/$/\ т $ time_namelookup/"

s   :substitute command 
/../../ :delimiter 
$   :end of line character. 
\t$time_namelookup: to append tab and the value. 
+0

Отлично работает Очень красивый подход. Не могли бы вы объяснить мне часть sed? – JMAD2016

+0

@ JMAD2016. Я обновил анс. Надеюсь, это может вам помочь. –