2

Below is my script to store the ping result on a text file, but the result stored in the output file is not complete, it is missing the last statement where ping stats are shown. Can anyone help in this?

Script:

#!/bin/bash
cp /dev/null /etc/scripts/ping-results.txt
fping -c5 -t50 192.168.10.1 >>/etc/scripts/ping-results.txt
#fping -c10 -t50 202.141.253.139 | perl -nle 'print scalar(localtime), " ", $_' >>/etc/scripts/ping-results.txt

Output when script is run:

root@noc-tst:/etc/scripts# ./ping-test.sh

192.168.176.54 : xmt/rcv/%loss = 5/5/0%, min/avg/max = 19.2/19.5/20.4

Output File (ping-results.txt):-
Tue Aug  6 00:12:10 2013 192.168.176.54 : [0], 84 bytes, 19.2 ms (19.2 avg, 0% loss)
Tue Aug  6 00:12:11 2013 192.168.176.54 : [1], 84 bytes, 19.3 ms (19.2 avg, 0% loss)
Tue Aug  6 00:12:13 2013 192.168.176.54 : [2], 84 bytes, 19.5 ms (19.3 avg, 0% loss)
Tue Aug  6 00:12:14 2013 192.168.176.54 : [3], 84 bytes, 19.3 ms (19.3 avg, 0% loss)
Tue Aug  6 00:12:15 2013 192.168.176.54 : [4], 84 bytes, 19.5 ms (19.4 avg, 0% loss)

1 Answer 1

4

The stats are written to stderr, you need to redirect it as well with 2>&1:

fping -c5 -t50 192.168.10.1 >>/etc/scripts/ping-results.txt 2>&1

Why are you first coping /dev/null to the file and then using the append redirect >>? Just use > and it will overwrite the file if it previously existed:

fping -c5 -t50 192.168.10.1 >/etc/scripts/ping-results.txt 2>&1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .