LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 08-05-2008, 09:52 AM   #1
cmfarley19
Member
 
Registered: Nov 2002
Location: Central VA
Distribution: Ubuntu/Debian
Posts: 228

Rep: Reputation: 32
Need help with a Bash Script


I've been having trouble with my ISP (Comcast) recently going up and down many times a day for many minutes a day.

I wrote a little script to go out to google every 5 minutes and it logs every time the connection is down. It will then log the next time the connection is back up.
Code:
     1  #!/bin/bash
     2  #       echo any arguments
     3
     4  DAT_FILE="/var/log/scripts/inetstate.dat"
     5  LOG_FILE="/home/cmfarley/connected.log"
     6  echo $1
     7
     8  # Get the date and time in spreadsheet friendly format
     9  DATE=$(date +%D' '%T)
    10
    11  # Try to connect.  If successful $CON="Google", else empty
    12  CON=$(lynx -dump -connect_timeout=10 google.com|grep -i Lucky|cut -d ' ' -f5)
    13
    14  # Get the last entry in the log
    15  TEMP=$( tail -1 $LOG_FILE |cut --complement -d ',' -f1|cut -d ' ' -f2)
    16
    17  # if an argurment (DEBUG) echo $TEMP
    18  if [ $1 ]
    19          then
    20          echo $TEMP
    21          echo $LOG_FILE
    22          VAR=`echo $1`
    23          echo $VAR
    24  else
    25          VAR="XXX"
    26  fi
    27   
    28  if [ $VAR == "-date" ]
    29          then
    30          date +%D  >> $LOG_FILE
    31  else
    32
    33          #if $CONNECT is empty...
    34          if [ -z $CON ]
    35                  then
    36                  echo "Not Connected" > $DAT_FILE 
    37                  # if $TEMP !empty, last attemp was connected, so log the disconnect
    38                  # else the last attempt was disconnected so do nothing
    39                  if [ $TEMP ]
    40                          then
    41                          echo "$DATE, " >> $LOG_FILE 
    42                  fi
    43
    44          else
    45                  echo "Connected to $CON" > $DAT_FILE 
    46                  # if $TEMP is empty, last attemp was !connected, so log the connection
    47                  # else the last attempt was connected so do nothing
    48                  if [ -z $TEMP ]
    49                           then
    50                           echo "$DATE" >> $LOG_FILE 
    51                  fi
    52
    53          fi
    54  fi      # -date
The output of the log file looks like this:
Code:
DOWN, UP
07/29/08
07/29/08 19:25:01, 
07/29/08 19:30:01
07/29/08 21:50:01, 
07/29/08 21:55:01
07/30/08
07/30/08 14:05:01, 
07/30/08 14:10:01
07/30/08 18:40:01, 
07/30/08 18:50:01
07/31/08
07/31/08 21:50:01, 
07/31/08 21:55:01
08/01/08
08/01/08 05:20:01, 
08/01/08 05:25:01
08/01/08 10:00:01, 
08/01/08 10:10:01
08/01/08 10:15:01, 
08/01/08 10:20:01
08/01/08 11:30:01, 
08/01/08 11:35:01
08/01/08 11:40:01, 
08/01/08 11:45:01
08/02/08
08/02/08 01:50:01, 
08/02/08 01:55:01
08/02/08 02:00:01, 
08/02/08 02:05:02
08/02/08 17:45:01, 
08/02/08 17:50:01
08/02/08 17:55:01, 
08/02/08 18:00:01
08/02/08 19:20:01, 
08/02/08 19:25:01
08/03/08
08/04/08
08/04/08 00:20:01, 
08/04/08 00:25:01
08/04/08 00:30:01, 
08/04/08 00:50:01
08/04/08 04:25:01, 
08/04/08 04:30:01
08/04/08 20:20:01, 
08/04/08 20:25:01
08/05/08
08/05/08 10:30:01, 
08/05/08 10:35:01
Line 41 outputs the down time stamp and line 50 outputs the up time stamp. What I want is the up time stamp to be on the same line as the down time stamp so they are in pairs.
Kind of like this:
Code:
...
08/04/08
08/04/08 00:20:01, 08/04/08 00:25:01
08/04/08 00:30:01, 08/04/08 00:50:01
08/04/08 04:25:01, 08/04/08 04:30:01
08/04/08 20:20:01, 08/04/08 20:25:01
08/05/08
08/05/08 10:30:01, 08/05/08 10:35:01
My grep, sed, and awk skills are little rusty.
Anybody have any suggestions?

Thanks,

Chris
 
Old 08-05-2008, 11:26 AM   #2
Kenhelm
Member
 
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 360

Rep: Reputation: 170Reputation: 170
This joins the next line to the end of a line ending in ', '
i.e. a line ending in <comma><space>
(It assumes that the next line does not also end in ', ')
Code:
sed '/, $/N; s/\n//' infile > outfile

Last edited by Kenhelm; 08-05-2008 at 11:42 AM. Reason: clarification
 
Old 08-05-2008, 12:12 PM   #3
cmfarley19
Member
 
Registered: Nov 2002
Location: Central VA
Distribution: Ubuntu/Debian
Posts: 228

Original Poster
Rep: Reputation: 32
That does the trick for post processing the file.
I'll need to modify that to work on the fly inside of my script so would you mind explaining a couple elements of that command?
What is the "$/N;"?
Does that line "read"
- Find a comma space at the end of a line "/, $/"
- Append the next line (N
- Replace the next line with nothing.

Just a guess?
 
Old 08-05-2008, 02:06 PM   #4
Kenhelm
Member
 
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 360

Rep: Reputation: 170Reputation: 170
'/, $/' Matches any line ending in ', '
'N' Means append the next line separated from the matched line by a newline \n.
's/\n//' Means remove the \n thereby turning two lines into one.

To do it from within the script try modifying line 41 to:-
Code:
echo -n "$DATE, " >> $LOG_FILE
With 'echo -n' the newline is suppressed.
 
Old 08-05-2008, 02:17 PM   #5
cmfarley19
Member
 
Registered: Nov 2002
Location: Central VA
Distribution: Ubuntu/Debian
Posts: 228

Original Poster
Rep: Reputation: 32
Ahh...

Thanks.

Chris
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
passing variable from bash to perl in a bash script quadmore Programming 6 02-21-2011 04:11 AM
Strange if statement behaviour when using bash/bash script freeindy Programming 7 08-04-2008 06:00 AM
set variables in a bash script; ansi PS1 color script donnied Programming 4 11-21-2007 11:33 AM
Bash script to create bash script jag7720 Programming 10 09-10-2007 07:01 PM
[bash] having trouble debugging this bash script. jons Programming 4 02-08-2007 06:51 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 07:10 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration