| View previous topic :: View next topic |
| Author |
Message |
Arthur_Dent LXF regular
Joined: Mon Jan 02, 2006 11:05 am Posts: 199 Location: London
|
Posted: Thu Sep 20, 2012 7:46 pm Post subject: Bash script - Truncating Variable |
|
|
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 |
|
 |
Arthur_Dent LXF regular
Joined: Mon Jan 02, 2006 11:05 am Posts: 199 Location: London
|
Posted: Sat Sep 22, 2012 7:16 am Post subject: |
|
|
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 |
|
 |
MartyBartfast LXF regular

Joined: Mon Aug 22, 2005 8:25 am Posts: 780 Location: Hants, UK
|
Posted: Sat Sep 22, 2012 7:48 am Post subject: |
|
|
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 previous topic :: View next topic |
|