Varying Vagrant Vagrants 提供脚本问题

Varying Vagrant Vagrants provision script issue

我正在尝试为 Browscap 编写配置脚本,我快完成了,我只是想看看在尝试重写 php.ini 文件时我做错了什么。

回购是 here

配置脚本如下所示

#!/usr/bin/env bash

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

DIR=`dirname [=10=]`

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

当规定结束并且我检查我的 php.ini 我得到

[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

所以我在 sed 命令中遗漏了一些东西。

但是什么?

MVE

如果你安装了 VVV,你可以使用 vagrant ssh

进入 shh

然后转到 /home/vagrant 并创建 text.txttest.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

这将导致 text.txt

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

重写此语句

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

只是

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

因为您可以在 shell 条件中直接使用 grep 的 return 代码。在前一种情况下,您错误地检查了 grep 的 return 代码以查看它是否成功,因为退出代码是由 shell.

处理的

(或) 或者你也可以做

grep -qe ";browscap" "${PHP_INI}"
rc=$?  

if [ $rc -eq 0 ]; then
    echo 'match found'
fi

因为 grep return 如果找到某物(零)与未找到某物(非零)则退出代码不同。在 if 语句中,零退出代码映射到 "true",非零退出代码映射到 false。

此外,您的 sed 语句应包括 = 之后的匹配部分,这需要像

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

根据您发布的 MCVE,您应该替换此代码块:

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

只有 1 行:

sudo sed -i 's|;?browscap =.*|browscap = /etc/php/7.0/mods-available/php_browscap.ini|' "${TEST_FILE}"