bsntech
May 11th, 2011, 02:30 AM
OK - I've been trying to get this working for a couple of hours now. I've confirmed that it is due to a wildcard in the variable.
General info:
I'm attempting to replace a specific line (say line 108) in the /etc/apache2/httpd.conf file.
So, I do a grep to find the line, run it through a series of other programs which then returns me the line number.
I'm searching for "website.domain.com":
LINEA=$(grep -n "website.domain.com" /etc/apache2/httpd.conf | cut -f1 -d: | head -1)
Above you can see that I'm searching for it and then assigning the line number to the LINEA variable.
Then, the REAL line I need is one line below that - so I increment LINEA by 1:
LINEA=$((LINEA + 1))
Now, I use sed to then read that line in:
CHANGE=$(sed -n "${LINEA}p" /etc/apache2/httpd.conf)
So now the contents of the line is loaded in the CHANGE variable.
Now, I want to append something to the line, so I do this:
NEWLINE="domain2.com *.domain2.com"
NEW="$CHANGE $NEWLINE"
So I am making a new variable - called NEWLINE - which will store the new information that I want to append to that line - and then I concatenate the contents of the line from the file with the newline - so then I have my full line that I need to write back to the file.
Alright - here is where the problem is. It should be as simple as doing this:
sed -e "${LINEA}s/$CHANGE/$NEW/g" -i /etc/apache2/httpd-test.conf
So basically the command above says to replace the contents of line number LINEA that contains the data in the $CHANGE variable with the data in the $NEW variable.
However, the $CHANGE variable has a * (asterisk) and I cannot escape it with a "\". So, nothing is done. If I manually type in the contents of the $CHANGE variable and escape the * (asterisk), it works. It is funny that the $NEW variable has at least two asterisks - sed has no problem with it.
So, because I clearly cannot escape the asterisk(s) in the $CHANGE variable, how can I get sed to ignore it and actually look at the line with the asterisk?
Thank you for any assistance.
General info:
I'm attempting to replace a specific line (say line 108) in the /etc/apache2/httpd.conf file.
So, I do a grep to find the line, run it through a series of other programs which then returns me the line number.
I'm searching for "website.domain.com":
LINEA=$(grep -n "website.domain.com" /etc/apache2/httpd.conf | cut -f1 -d: | head -1)
Above you can see that I'm searching for it and then assigning the line number to the LINEA variable.
Then, the REAL line I need is one line below that - so I increment LINEA by 1:
LINEA=$((LINEA + 1))
Now, I use sed to then read that line in:
CHANGE=$(sed -n "${LINEA}p" /etc/apache2/httpd.conf)
So now the contents of the line is loaded in the CHANGE variable.
Now, I want to append something to the line, so I do this:
NEWLINE="domain2.com *.domain2.com"
NEW="$CHANGE $NEWLINE"
So I am making a new variable - called NEWLINE - which will store the new information that I want to append to that line - and then I concatenate the contents of the line from the file with the newline - so then I have my full line that I need to write back to the file.
Alright - here is where the problem is. It should be as simple as doing this:
sed -e "${LINEA}s/$CHANGE/$NEW/g" -i /etc/apache2/httpd-test.conf
So basically the command above says to replace the contents of line number LINEA that contains the data in the $CHANGE variable with the data in the $NEW variable.
However, the $CHANGE variable has a * (asterisk) and I cannot escape it with a "\". So, nothing is done. If I manually type in the contents of the $CHANGE variable and escape the * (asterisk), it works. It is funny that the $NEW variable has at least two asterisks - sed has no problem with it.
So, because I clearly cannot escape the asterisk(s) in the $CHANGE variable, how can I get sed to ignore it and actually look at the line with the asterisk?
Thank you for any assistance.