Bash: how to check and insert a string within a block in a file
The following will replace anything inside the block "IfModule - /IfModule" with your text "AddHandler application/x-httpd-php .php". So regardless if the comment (#) is found or if nothing is found the block will have the correct information.
Also it is IMPORTANT to note that this is an inline edit. The file you are wanting changed will be changed immediately upon running this script with NO BACKUP MADE. PRIOR TO TESTING ANYTHING PLEASE BACKUP YOUR FILE.
sed -i '/<\/IfModule>/,/<IfModule>/!s/ \.\.\..*/ \.\.\.\n AddHandler application\/x-httpd-php .php/g' /yourfile
If you are working on OS X, you may want to try the following script instead
#!/bin/bashCR="$(printf '\r')"sed -i '' "/<\/IfModule>/,/<IfModule>/!s/ \.\.\..*/ \.\.\.${CR} AddHandler application\/x-httpd-php .php/g" /yourfile
Example of Starting file:
<IfModule> ...</IfModule><NOTHING TO CHANGE> ...</NOTHING TO CHANGE><IfModule> ...# AddHandler application/x-httpd-php .php</IfModule><NO CHANGE> ... NO CHANGE</NO CHANGE>
Results:
<IfModule> ... AddHandler application/x-httpd-php .php</IfModule><NOTHING TO CHANGE> ...</NOTHING TO CHANGE><IfModule> ... AddHandler application/x-httpd-php .php</IfModule><NO CHANGE> ... NO CHANGE</NO CHANGE>