0

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

回购在这里

配置脚本如下所示

#!/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

当规定结束时,我检查我的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,您可以使用 shhvagrant ssh

然后去/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
4

2 回答 2

1

根据您发布的 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}"
于 2017-12-26T16:47:28.537 回答
1

重写此语句

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

只是

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

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

(或)或者你也可以这样做

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

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

因为grep如果找到某些东西(零)与没有找到某些东西(非零),则返回不同的退出代码。在 if 语句中,零退出代码映射为“真”,非零退出代码映射为假。

此外,您的sed声明应包括匹配部分,之后=需要完成

sed -i "s|;browscap =.*|browscap = /etc/php/${PHP_VER}/mods-available/php_browscap.ini|g" "/etc/php/${PHP_VER}/fpm/php.ini"
于 2017-12-26T12:41:25.120 回答