Find the answer to your Linux question:
Results 1 to 2 of 2
Hi I'm trying to write a bash/shell script to ping a server and redirect part of the output to a file. The following code does the job: Code: #!/bin/sh date ...
  1. #1
    Just Joined!
    Join Date
    Oct 2004
    Posts
    3

    Bash script to log ping requests

    Hi

    I'm trying to write a bash/shell script to ping a server and redirect part of the output to a file.

    The following code does the job:
    Code:
    #!/bin/sh
    date >> /home/matt/ping.log
    echo ': ' >> /home/matt/ping.log
    ping -qc 5 example.com | grep '5 packets' >> /home/matt/ping.log
    exit 0
    But how do I put the output on a single line, e.g:
    Code:
    Sat Sep  8 20:57:45 BST 2007: 5 packets transmitted, 5 received, 0% packet loss, time 4002ms

  2. #2
    Just Joined!
    Join Date
    Oct 2004
    Posts
    3
    I figured it out myself:
    Code:
    #!/bin/sh
    NOW=$(date +"%T %m/%d/%Y")
    PING=$(ping -qc 5 example.com | grep '5 packets')
    echo $NOW: $PING >> /home/matt/ping.log
    exit 0

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...