943,789 Members | Top Members by Rank

Ad:
Oct 7th, 2008
0

newline characters disappearing

Expand Post »
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
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
shwick is offline Offline
63 posts
since Oct 2008
Oct 7th, 2008
0

Re: newline characters disappearing

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}')
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Oct 7th, 2008
0

Re: newline characters disappearing

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.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
shwick is offline Offline
63 posts
since Oct 2008
Oct 8th, 2008
0

Re: newline characters disappearing

No.
You'll have to use a temp file.

Or write the whole thing in perl, then you can do whatever you want.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Oct 8th, 2008
0

Re: newline characters disappearing

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
Reputation Points: 46
Solved Threads: 28
Junior Poster
Gromit is offline Offline
183 posts
since Sep 2008
Oct 8th, 2008
0

Re: newline characters disappearing

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.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
shwick is offline Offline
63 posts
since Oct 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Shell Scripting Forum Timeline: finding files for specified date
Next Thread in Shell Scripting Forum Timeline: Downloading attachments from an email through a shell script.





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC