newline characters disappearing

Reply

Join Date: Oct 2008
Posts: 46
Reputation: shwick is an unknown quantity at this point 
Solved Threads: 0
shwick shwick is offline Offline
Light Poster

newline characters disappearing

 
0
  #1
Oct 7th, 2008
Running ubuntu 8.04

I built this off of the tripwire daily cron script. I'm trying to check the number of violations.

There's a problem when $tripResult is instantiated with the tripwire report- it doesn't have any newline characters.

This causes grep to not get the line with the number of violations, it just gets the entire report.

#!/bin/sh -e

tripwire=/usr/sbin/tripwire

[ -x $tripwire ] || exit 0

umask 027

tripResult=$($tripwire --check --quiet)

tripViolations=$(echo $tripResult | grep "Total violations found" | awk '{print $4}')

exit 0
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,851
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: newline characters disappearing

 
0
  #2
Oct 7th, 2008
Because storing the result in a shell variable always strings out redundant white space.

If you want newlines, then pipe it direct.

tripViolations=$($tripwire --check --quiet | awk '/Total violations found/ {print $4}')
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 46
Reputation: shwick is an unknown quantity at this point 
Solved Threads: 0
shwick shwick is offline Offline
Light Poster

Re: newline characters disappearing

 
0
  #3
Oct 7th, 2008
Thanks that works.

But I want to use the results in two different places- first check for violations, then email the full report to me, in the same script.

I could issue tripwire --check twice(bad), I could write the results to a file then read it twice(ok...), but is there some option that will keep redundant white space in the shell variable?
Last edited by shwick; Oct 7th, 2008 at 6:35 pm.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,851
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: newline characters disappearing

 
0
  #4
Oct 8th, 2008
No.
You'll have to use a temp file.

Or write the whole thing in perl, then you can do whatever you want.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 61
Reputation: Gromit is an unknown quantity at this point 
Solved Threads: 7
Gromit's Avatar
Gromit Gromit is offline Offline
Junior Poster in Training

Re: newline characters disappearing

 
0
  #5
Oct 8th, 2008
Doesn't tripwire write a to a report file by default? Maybe you could get the results you're looking for by running tripwire and then parsing the report?

Otherwise, I think you're just missing some quotes to hold everything together Try this!

Shell Scripting Syntax (Toggle Plain Text)
  1. #!/bin/sh -e
  2.  
  3. tripwire=/usr/sbin/tripwire
  4.  
  5. [ -x $tripwire ] || exit 0
  6.  
  7. umask 027
  8.  
  9. tripResult="$($tripwire --check --quiet)"
  10.  
  11. tripViolations=$(echo "$tripResult" | awk '/Total.violations.found/ {print $4}')
  12.  
  13. exit 0

(I took some liberties with your grep|awk arrangement... why use 2 cmds when one will do!

Another way to do this would be with a temporary file, like Salem said. You could do something like this:

Shell Scripting Syntax (Toggle Plain Text)
  1. #!/bin/sh -e
  2.  
  3. tripwire=/usr/sbin/tripwire
  4.  
  5. ## configure the log file as a variable
  6. logfile=/tmp/tw.log
  7.  
  8. [ -x $tripwire ] || exit 0
  9.  
  10. umask 027
  11.  
  12. $tripwire --check --quiet > $logfile
  13.  
  14. tripResult=$(cat $logfile)
  15.  
  16. tripViolations=$(awk '/Total.violations.found/ {print $4}' $logfile)
  17.  
  18. rm $logfile
  19.  
  20. exit 0

Or you might want to choose to keep those log files, and timestamp them.

Shell Scripting Syntax (Toggle Plain Text)
  1. logfile=/tmp/tw-$(date +%D-%T).log
  2. # comment out the 'rm' at the end ;)
  3. # rm $logfile


I hope this helps! Sorry if that's too much info... or not enough...

-G
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 46
Reputation: shwick is an unknown quantity at this point 
Solved Threads: 0
shwick shwick is offline Offline
Light Poster

Re: newline characters disappearing

 
0
  #6
Oct 8th, 2008
See you shouldn't have done that cause now I used your solution instead of learning perl cause I'm lazy.

But seriously thanks it works great. For the log file I had to change %D to %F.

Edit: it was the second solution that worked.
Last edited by shwick; Oct 8th, 2008 at 10:10 pm.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the Shell Scripting Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC