It is currently Sun May 20, 2018 11:39 am

while loop string matching

All times are UTC - 6 hours

Post new topic Reply to topic  [ 3 posts ] 
Author Message
 PostPosted: Wed Jun 27, 2012 7:56 am   

Joined: Wed May 19, 2010 9:56 am
Posts: 21
Before I loose my ever-loving-mind, can someone help me determine what I am doing wrong here. This was suppose to be an easy and quick script.

I got a PKI certificate bundle. Here is an shorten truncated example:


There are multiple "-----BEGIN CERTIFICATE-----" and "-----END CERTIFICATE-----" strings in the file.
I echo each line to a file and when the "-----END CERTIFICATE-----" is seen the script will create a new file and keep repeating the process of finding the "END" and creating new files until the input ends.

Here is my simple working script. I usually start scripts out very simple and work towards complexity as I confirm each step:

while read STRING
   echo "$STRING" >> certfile1.out
   echo "$STRING"  # Echoing for testing and watching for END string.
   if [ "$STRING" = "-----END CERTIFICATE-----" ]; then
      echo END found
done < inputcertfile.crt

As I said I have started out very simple by just trying to output to a file and exit when the END string is found. In my if statement I have tried all sorts of matching syntax such as:

[[ "$STRING" == "*[E][N][D]*" ]]
[ "$STRING" =~ "-----END CERTIFICATE-----" ]
[[ "$STRING" = "-----[E][N][D]*" ]]
plus a handful of other attempts using = or =~ or == along with single bracket and double bracket attempts.

So what am I missing? Any ideas on grabbing the END string and exiting. I'll fix the file redirection later, I just want the thing to exit. :) :-/

 PostPosted: Wed Jun 27, 2012 8:35 am   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 643

the right hand side (the regex) of the =~ operator inside [[ command should not be quoted.
shopt -s nocasematch
match="-{5}end certificate-{5}"
if [[ $string =~ $match ]]; then echo "\"END\" found"; fi

edit: no need to protect white spaces when regex is defined inside a quoted variable.

Last edited by Watael on Wed Jun 27, 2012 10:16 am, edited 2 times in total.

 PostPosted: Wed Jun 27, 2012 9:00 am   

Joined: Wed May 19, 2010 9:56 am
Posts: 21
Thanks Watael. My bash version does not support nocasematch. It did not work in the script nor was it mentioned in the manpage for this particular server. No biggy, I'll stick with case sensitive syntax.

I was quoting the left side of the regex due to a white space problem I had in another iteration of my script when setting a variable and just forgot all about unquoting it. Thanks for that reminder.

Also thanks for the short hand usage for pattern repetition. -{5} sure looks better than -----.

It is odd how one can set so long looking at something and not see it. You've saved my mind from further turmoil.

Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 6 hours

Who is online

Users browsing this forum: No registered users and 7 guests

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 post attachments in this forum

Jump to:  

BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group