Linux Format forums Forum Index Linux Format forums
Help, discussion, magazine feedback and more
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Bash script - Truncating Variable

 
Post new topic   Reply to topic    Linux Format forums Forum Index -> Help!
View previous topic :: View next topic  
Author Message
Arthur_Dent
LXF regular


Joined: Mon Jan 02, 2006 11:05 am
Posts: 219
Location: London

PostPosted: Thu Sep 20, 2012 7:46 pm    Post subject: Bash script - Truncating Variable Reply with quote

Hello all,

I don't speak fluent bash. I can just about order a beer in a bar, but I am a long way off being able to chat with a local.

I use a script called clamassassin to pipe emails through clamd. It marks up the email headers (using formail) with the results of the scan. The problem is that the script has not been developed since 2007 and the author cannot be contacted. There is a fault with the script and I am trying to fix it myself but my bash scripting skills are letting me down.

The script first obtains the clamd version but calling "clamdscan -V" and reading it into a variable called $CLAMVERS. If there is a problem with clamd (daemon not running - file permission... whatever...) it returns something like:
Code:
ERROR: Can't connect to clamd: Permission denied
ClamAV 0.97.5

Note that there is a line-break in there. When the script goes on to put this into the headers using formail the line-break corrupts the email.

I am trying to fix this, but still retain the error detail, by cutting off the line break and the ClamAV... part.

This is where I need help...

Code:
# Set version header string
CLAMVERS=`${CLAMSCAN} -V --stdout`

if [[ "$CLAMVERS" == *ERROR* ]] # Not how it works in the script, but OK for testing
then
   ERRORMSG=`echo ${CLAMVERS} | sed -e "/ClamAV/!d" -e "s/.* //"`
   echo "Error = :${ERRORMSG}"

else
   echo "ClamVersion = $CLAMVERS"
fi

Essentially I want to cut off anything after the ClamAV part and the character before that (the line break).

I'm stuck.

Any fluent bash speakers care to help me?

Thanks in advance.

Mark
Back to top
View user's profile Send private message
Arthur_Dent
LXF regular


Joined: Mon Jan 02, 2006 11:05 am
Posts: 219
Location: London

PostPosted: Sat Sep 22, 2012 7:16 am    Post subject: Reply with quote

Replying to my own post:

I have found a solution to this. I'm sure there are better ways, but this works.

Code:
# Define the newline character to be found and stripped out:
NL="
"
# I tried variations of "\n" but that didn't seem to work.
CLAMERROR=${CLAMVERS%$NL*}


The %{string}* thing means strip out everything after and including {string}. There is an equivalent directive #*{string} which means strip out everything up to and including {string}. I had never come across the % and # directives in string handling before. It does what I want anyway...
Back to top
View user's profile Send private message
MartyBartfast
LXF regular


Joined: Mon Aug 22, 2005 8:25 am
Posts: 812
Location: Hants, UK

PostPosted: Sat Sep 22, 2012 7:48 am    Post subject: Reply with quote

good find, could come in very handy, I've used awk for such things but always thought it was a bit cluncky.
_________________
I have been touched by his noodly appendage.
Back to top
View user's profile Send private message
View previous topic :: View next topic  
Display posts from previous:   
Post new topic   Reply to topic    Linux Format forums Forum Index -> Help! All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Linux Format forums topic RSS feed 


Powered by phpBB © 2001, 2005 phpBB Group


Copyright 2011 Future Publishing, all rights reserved.


Web hosting by UKFast