سؤال

I am moving away from AMPPS / MAMP and looking to build a dev environment as close to the production environment as possible.

As such, I am using Vagrant / VirtualBox on my Mac with CentOS 6.4 64bit os box installed.

In my vagrant file, I have a provisioning script:

config.vm.provision :shell, :path => "bootstrap.sh"

And at the moment, my bootstrap.sh looks as follows:

#!/usr/bin/env bash

# Install the remi repos
sudo rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-6.rpm

# Update the repositories
sudo yum -y update

sudo yum -y --enablerepo=remi,remi-php55 install php-pecl-apc php-cli php-pear php-pdo php-mysqlnd php-pecl-memcache php-pecl-memcached php-gd php-mbstring php-mcrypt php-xml

Now I have my apache server installed, I want my bash script to edit the /etc/httpd/conf/httpd.conf file and change AllowOverride None to AllowOverride All in my html directory.

<Directory "/var/www/html">

...

    Options Indexes FollowSymLinks

...

    # Need to change this to AllowOverride All via bash script
    AllowOverride None

...

    Order allow,deny
    Allow from all

</Directory>
هل كانت مفيدة؟

المحلول 2

Here is a nice oneliner for you:

sed "$(grep -n "AllowOverride None" input.file |cut -f1 -d:)s/.*/AllowOverride All/" input.file > output.file

For the breakdown:

  • grep -n "AllowOverride None" input.file returns the lines that match the pattern, preceeded by the line number
  • cut -f1 -d: cuts the string and returns the first number it encounters
  • sed "$LINE_NUMBERs/.*/AllowOverride All/" input.file puts "AllowOverride All" at the line n° $LINE_NUMBER

You then just have to redirect the output to the file you want.

Et voila !

نصائح أخرى

Use Linux SED command to search & replace a line between two patterns

Another nice solution is to use the SED command to search & replace a line between two patterns. In your case the patterns would be:

'<Directory "/var/www/html">' and </directory>'

This way you replace only the line 'AllowOverride None' inside your html directory (<Directory "/var/www/html">) and not every line that contains 'AllowOverride None' in your httpd.conf file.

The command SED supports patterns ranges in this form:

sed '/startpattern/,/endpattern/ <sed-commands>' file

In your case, this becomes:

sed -i '/<Directory "\/var\/www\/html">/,/<\/Directory>/ s/AllowOverride None/AllowOverride all/' /etc/httpd/conf/httpd.conf

The -i option is used to edit files in place. If you don’t use the option -i, then the modified output will be printed on the screen and the file wouldn’t be modified!

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top