我正在尝试为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.txt
和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
这将导致text.txt
[browscap]
browscap = /etc/php/7.0/mods-available/php_browscap.ini extra/browscap.ini