Tuesday, 4 December 2018

Using the Stream Editor (sed) with Bash variables

I've got a shell script to setup IBM HTTP Server (IHS), which includes a question: -

if [ -z "$1" ]
    echo "For what product are you creating this ?"
    exit 1
 and then uses the Stream Editor (sed) to use this variable : -

sed -i'' 's/PidFile\ logs/PidFile\ ${Product}\/logs/g' /opt/ibm/HTTPServer/${Product}/conf/httpd.conf
sed -i'' 's/ErrorLog\ logs/ErrorLog\ ${Product}\/logs/g' /opt/ibm/HTTPServer/${Product}/conf/httpd.conf
sed -i'' 's/CustomLog\ logs/CustomLog\ ${Product}\/logs/g' /opt/ibm/HTTPServer/${Product}/conf/httpd.conf
Sadly, this didn't quite work ....

Instead, I ended up with this: -
PidFile ${Product}/logs/httpd.pid
rather than this: -
PidFile WAS/logs/httpd.pid
 Thankfully, it was a quick fix - I just needed to change my Bash script to this: -

sed -i'' "s/PidFile\ logs/PidFile\ ${Product}\/logs/g" /opt/ibm/HTTPServer/${Product}/conf/httpd.conf
sed -i'' "s/ErrorLog\ logs/ErrorLog\ ${Product}\/logs/g" /opt/ibm/HTTPServer/${Product}/conf/httpd.conf
sed -i'' "s/CustomLog\ logs/CustomLog\ ${Product}\/logs/g" /opt/ibm/HTTPServer/${Product}/conf/httpd.conf

In other words, I replaced the single quotes around the s/from/to/g section of the Sed script with double quotes ....

Thanks to this: -

How do I use variables in a sed command?

for inspiration.

No comments:

Grokking grep

A colleague was tinkering with grep  and, thanks to him, I discovered a bit more about the trusty little utility. I had not really explored ...