带有反斜杠的sed脚本可在控制台或独立脚本中运行,但不适用于Vagrantfile

时间:2017-05-01 20:23:15

标签: bash vagrant

我有这一行:

sed -i 's/^\$cfg\['\''Servers'\''\]\[\$i\]\['\''AllowNoPassword'\''\] = .*/\$cfg\['\''Servers'\''\]\[\$i\]\['\''AllowNoPassword'\''\] = true;/' config.inc.php

将我的PHPMyAdmin config.inc.php AllowNoPassword编辑为true非常简单(当然这是一个开发环境)。

它在控制台中完美运行,但在使用vagrant的脚本文件中,它根本没有。

我确实认为这与'有关,但我无法理解其中的区别。

这里发生了什么以及如何解决?

修改

这是一个完整的例子,减去一些逻辑来简化它并删除一些私人细节等:

# -*- mode: ruby -*-
# vi: set ft=ruby :

# All Vagrant configuration is done below. The "2" in Vagrant.configure
# configures the configuration version (we support older styles for
# backwards compatibility). Please don't change it unless you know what
# you're doing.
Vagrant.configure("2") do |config|
  # The most common configuration options are documented and commented below.
  # For a complete reference, please see the online documentation at
  # https://docs.vagrantup.com.

  # Every Vagrant development environment requires a box. You can search for
  # boxes at https://atlas.hashicorp.com/search.
  config.vm.box = "ubuntu/xenial64"

  # Disable automatic box update checking. If you disable this, then
  # boxes will only be checked for updates when the user runs
  # `vagrant box outdated`. This is not recommended.
  # config.vm.box_check_update = false

  # Create a forwarded port mapping which allows access to a specific port
  # within the machine from a port on the host machine. In the example below,
  # accessing "localhost:8080" will access port 80 on the guest machine.
  # NOTE: This will enable public access to the opened port
  # config.vm.network "forwarded_port", guest: 80, host: 8080

  # Create a forwarded port mapping which allows access to a specific port
  # within the machine from a port on the host machine and only allow access
  # via 127.0.0.1 to disable public access
  config.vm.network "forwarded_port", guest: 80, host: 8080, host_ip: "127.0.0.1"
  config.vm.network "forwarded_port", guest: 443, host: 4343, host_ip: "127.0.0.1"

  # Create a private network, which allows host-only access to the machine
  # using a specific IP.
  # config.vm.network "private_network", ip: "192.168.33.10"

  # Create a public network, which generally matched to bridged network.
  # Bridged networks make the machine appear as another physical device on
  # your network.
  # config.vm.network "public_network"

  # Share an additional folder to the guest VM. The first argument is
  # the path on the host to the actual folder. The second argument is
  # the path on the guest to mount the folder. And the optional third
  # argument is a set of non-required options.
  # config.vm.synced_folder "../data", "/vagrant_data"

  # Provider-specific configuration so you can fine-tune various
  # backing providers for Vagrant. These expose provider-specific options.
  # Example for VirtualBox:
  #
  # config.vm.provider "virtualbox" do |vb|
  #   # Display the VirtualBox GUI when booting the machine
  #   vb.gui = true
  #
  #   # Customize the amount of memory on the VM:
  #   vb.memory = "1024"
  # end
  #
  # View the documentation for the provider you are using for more
  # information on available options.

  # Define a Vagrant Push strategy for pushing to Atlas. Other push strategies
  # such as FTP and Heroku are also available. See the documentation at
  # https://docs.vagrantup.com/v2/push/atlas.html for more information.
  # config.push.define "atlas" do |push|
  #   push.app = "YOUR_ATLAS_USERNAME/YOUR_APPLICATION_NAME"
  # end

  # Enable provisioning with a shell script. Additional provisioners such as
  # Puppet, Chef, Ansible, Salt, and Docker are also available. Please see the
  # documentation for more information about their specific syntax and use.
  config.vm.provision "shell", inline: <<-SHELL

    add-apt-repository ppa:ondrej/php

    apt-get update > /dev/null

    yes | apt-get install zip

    yes | apt-get install php7.1-fpm \
    php7.1-curl \
    php7.1-gd \
    php7.1-mysql \
    php7.1-mbstring \
    php7.1-xml \
    php7.1-mcrypt \
    php7.1-soap \
    php7.1-dev

    # Set the right user for PHP7 ("ubuntu" user)
    sed -i -e "s/www-data/ubuntu/g" /etc/php/7.1/fpm/pool.d/www.conf

    # Fix error reporting so it is consistent with live server
    sed -i 's/^error_reporting = .*/error_reporting = E_ALL/' /etc/php/7.1/fpm/php.ini
    sed -i 's/^error_reporting = .*/error_reporting = E_ALL/' /etc/php/7.1/cli/php.ini

    yes | apt-get install php-pear
    pecl install xdebug

    # Add xdebug to PHP runtime
    echo 'zend_extension=xdebug.so' >> /etc/php/7.1/fpm/php.ini
    echo 'zend_extension=xdebug.so' >> /etc/php/7.1/cli/php.ini

    service php7.1-fpm restart

    debconf-set-selections <<< 'mysql-server mysql-server/root_password password root'
    debconf-set-selections <<< 'mysql-server mysql-server/root_password_again password root'
    yes | apt-get -y install mysql-server
    mysqladmin -u root -p'root' password ''
  SHELL

  $script = <<-SCRIPT
    cp config.sample.inc.php config.inc.php
    sed -i 's/^\$cfg\['\''Servers'\''\]\[\$i\]\['\''AllowNoPassword'\''\] = .*/\$cfg\['\''Servers'\''\]\[\$i\]\['\''AllowNoPassword'\''\] = true;/' config.inc.php
  SCRIPT
  config.vm.provision "shell", inline: $script, privileged: false
  #config.vm.provision :shell, path: "bootstrap.sh"
end

1 个答案:

答案 0 :(得分:2)

使用转义 heredoc:

  $script = <<-'SCRIPT'
    cp config.sample.inc.php config.inc.php
    sed -i 's/^\$cfg\['\''Servers'\''\]\[\$i\]\['\''AllowNoPassword'\''\] = .*/\$cfg\['\''Servers'\''\]\[\$i\]\['\''AllowNoPassword'\''\] = true;/' config.inc.php
  SCRIPT

围绕SCRIPT的引号向Ruby解释器表明所有内容都应该是文字的 - 按照给定的精确而不是倾向于扩展。 (这些引用在shell heredocs中具有相同的含义)。