bashshellvagrantvvv-wordpress

Varying Vagrant Vagrants provision script issue


I'm trying to write a provision script for Browscap, and I'm almost there, I just want to see what I'm doing wrong when trying to rewrite php.ini file.

The repo is here.

The provision script looks like this

#!/usr/bin/env bash

# Prettyfiers
BLUE='\033[0;36m'
NC='\033[0m' # No Color

DIR=`dirname $0`

echo "${BLUE}Setting up Browsecap${NC}"

# Check PHP version
PHP_VER=`php -r \@phpinfo\(\)\; | grep 'PHP Version' -m 1 | grep -Po -m 1 '(\d+\.\d+)' | head -1`
MIN_REQ="5.3"

if (( $(echo "${PHP_VER} < ${MIN_REQ}" |bc -l) )); then
  echo "${BLUE}The PHP version is lower than 5.3 so browscap won't work. Please upgrade your PHP version to higher than 5.3${NC}"
  exit 0
fi
echo "${BLUE}PHP version is${NC}" ${PHP_VER}

BROWSE_INI="/etc/php/${PHP_VER}/mods-available/php_browscap.ini"

# Check if browscap is already installed/set up
if [[ -f "${BROWSE_INI}" ]]; then
  echo "${BLUE}Browscap is already installed${NC}"
else
  # Set the browscap.ini and the php extension
  touch "${BROWSE_INI}"
  cp "php_browscap.ini" "${BROWSE_INI}"
  echo "${BLUE}Browscap copied${NC}"
fi

PHP_INI="/etc/php/${PHP_VER}/fpm/php.ini"

# Check if php.ini exists before replacing
if [[ -f "${PHP_INI}" ]]; then
  echo "${BLUE}php.ini exists${NC}"
  # Check if the default value exists - by default it should be ;browscap = extra/browscap.ini
  # If it doesn't then find browscap = and replace it with the correct one
  if [ "$(grep -qe ";browscap" "${PHP_INI}")" ]; then
    sudo sed -i "s|;browscap =|browscap = /etc/php/${PHP_VER}/mods-available/php_browscap.ini|g" "/etc/php/${PHP_VER}/fpm/php.ini"
  else
    sudo sed -i "s|browscap =|browscap = /etc/php/${PHP_VER}/mods-available/php_browscap.ini|g" "/etc/php/${PHP_VER}/fpm/php.ini"
  fi
  echo "${BLUE}php.ini changed${NC}"
else
  echo "${BLUE}php.ini doesn't exist${NC}"
fi

When the provision ends and I check my php.ini I get

[browscap]
; http://php.net/browscap
;browscap = /etc/php/7.0/mods-available/php_browscap.ini /etc/php/7.0/mods-available/php_browscap.ini extra/browscap.ini

So I'm missing something in my sed command.

But what?

MVE

If you have VVV installed you can go to shh with vagrant ssh

Then go to /home/vagrant and create text.txt and test.sh

text.txt

[browscap]
;browscap = extra/browscap.ini

test.sh

#!/usr/bin/env bash

TEST_FILE="/home/vagrant/text.txt"

# Check if php.ini exists before replacing
if [[ -f "${TEST_FILE}" ]]; then
  echo "text.txt exists"
  # Check if the default value exists - by default it should be ;browscap = extra/browscap.ini
  # If it doesn't then find browscap = and replace it with the correct one
  if grep -q ";browscap" "${TEST_FILE}"; then
    sudo sed -i "s|;browscap =|browscap = /etc/php/7.0/mods-available/php_browscap.ini|g" "${TEST_FILE}"
  else
    sudo sed -i "s|browscap =|browscap = /etc/php/7.0/mods-available/php_browscap.ini|g" "${TEST_FILE}"
  fi
  echo "text.txt changed"
else
  echo "text.txt doesn't exist"
fi

This will result in text.txt

[browscap]
browscap = /etc/php/7.0/mods-available/php_browscap.ini extra/browscap.ini

Solution

  • Rewrite this statement

    if [ "$(grep -qe ";browscap" "${PHP_INI}")" ]; then
    

    to just

    if grep -q ";browscap" "${PHP_INI}"; then
    

    since you can directly use grep's return code in shell conditionals. The reason being in the former case, you are incorrectly checking the return code of grep to see if it succeeded, because the exit code is processed by the shell.

    (or) alternatively you could also do

    grep -qe ";browscap" "${PHP_INI}"
    rc=$?  
    
    if [ $rc -eq 0 ]; then
        echo 'match found'
    fi
    

    because grep returns a different exit code if its found something (zero) vs. if it hasn't found something (non-zero). In an if statement, a zero exit code is mapped to "true" and a non-zero exit code is mapped to false.

    Also your sed statement should include the matching part after = which needs to be done as

    sed -i "s|;browscap =.*|browscap = /etc/php/${PHP_VER}/mods-available/php_browscap.ini|g" "/etc/php/${PHP_VER}/fpm/php.ini"