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" ]
  then
    echo "For what product are you creating this ?"
    exit 1
fi
 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:

Visual Studio Code - Wow 🙀

Why did I not know that I can merely hit [cmd] [p]  to bring up a search box allowing me to search my project e.g. a repo cloned from GitHub...