php env.php edit from bash script using sed, awk o

2019-07-25 07:11发布

问题:

Using a bash script, I am trying to edit a Magento 2 env.php config file. The bash script first performs some other operations. It needs to change this part in the config file on line 11-14:

  'session' => 
  array (
    'save' => 'files',
  ),

Into this:

'session' => 
   array (
   'save' => 'redis',
   'redis' => 
      array (
        'host' => '127.0.0.1',
        'port' => '6379',
        'password' => '',
        'timeout' => '2.5',
        'persistent_identifier' => '',
        'database' => '0',
        'compression_threshold' => '2048',
        'compression_library' => 'gzip',
        'log_level' => '1',
        'max_concurrency' => '6',
        'break_after_frontend' => '5',
        'break_after_adminhtml' => '30',
        'first_lifetime' => '600',
        'bot_first_lifetime' => '60',
        'bot_lifetime' => '7200',
        'disable_locking' => '0',
        'min_lifetime' => '60',
        'max_lifetime' => '2592000'
    )
  ),
'cache' =>
array(
   'frontend' =>
   array(
      'default' =>
      array(
         'backend' => 'Cm_Cache_Backend_Redis',
         'backend_options' =>
         array(
            'server' => '127.0.0.1',
            'database' => '0',
            'port' => '6379'
            ),
    ),
    'page_cache' =>
    array(
      'backend' => 'Cm_Cache_Backend_Redis',
      'backend_options' =>
       array(
         'server' => '127.0.0.1',
         'port' => '6379',
         'database' => '1',
         'compress_data' => '0'
       )
    )
  )
),

I have tried to achieve using various ways with both sed and awk and already spent multiple hours on it, but I cannot figure out what I am doing wrong. I had no prior experience with both sed, awk or shell scripts in general. After I couldn't achieve what I really wanted, a "search and replace" which you can do in every GUI text editor, I ended up deleting the old contents of 'session' with sed by referencing the line numbers and trying to add the right contents using awk. I already tried using backslashes at the end of every sentence. This is the script I wrote with both sed and awk, after first trying to get this right using:

sed -i -e 12d -e 13d -e 14d /var/www/ecom/app/etc/env.php
awk '
{ print }
/  'session' => / {
print "  array ( "
print "  'save' => 'redis',"
print "  'redis' =>"
print "     array ("
print "       'host' => '127.0.0.1',"
print "       'port' => '6379',"
print "       'password' => '',"
print "       'timeout' => '2.5',"
print "       'persistent_identifier' => '',"
print "       'database' => '0',"
print "       'compression_threshold' => '2048',"
print "       'compression_library' => 'gzip',"
print "       'log_level' => '1',"
print "       'max_concurrency' => '6',"
print "       'break_after_frontend' => '5',"
print "       'break_after_adminhtml' => '30',"
print "       'first_lifetime' => '600',"
print "       'bot_first_lifetime' => '60',"
print "       'bot_lifetime' => '7200',"
print "       'disable_locking' => '0',"
print "       'min_lifetime' => '60',"
print "       'max_lifetime' => '2592000'"
print "   )"
print " ),"' /var/www/html/app/etc/env.php

However, the result I get is:

awk: cmd. line:27: print " ), "
awk: cmd. line:27:             ^ unexpected newline or end of string

My guess is that I am not exiting properly, but numerous Google searches haven't resulted in finding a solution.

This is another attempt using cat and sed, it doesn't work either and results in an empty file:

cat <<EOF <(sed -i '1,/  'session' => \n  array (\n    'save' => 'files',\n  ),/d' /var/www/html/app/etc/env.php) >> /var/www/html/app/etc/env.php
'session' =>
   array (
   'save' => 'redis',
   'redis' =>
      array (
        'host' => '127.0.0.1',
        'port' => '6379',
        'password' => '',
        'timeout' => '2.5',
        'persistent_identifier' => '',
        'database' => '0',
        'compression_threshold' => '2048',
        'compression_library' => 'gzip',
        'log_level' => '1',
        'max_concurrency' => '6',
        'break_after_frontend' => '5',
        'break_after_adminhtml' => '30',
        'first_lifetime' => '600',
        'bot_first_lifetime' => '60',
        'bot_lifetime' => '7200',
        'disable_locking' => '0',
        'min_lifetime' => '60',
        'max_lifetime' => '2592000'
    )
  ),
'cache' =>
array(
   'frontend' =>
   array(
      'default' =>
      array(
         'backend' => 'Cm_Cache_Backend_Redis',
         'backend_options' =>
         array(
            'server' => '127.0.0.1',
            'port' => '6379'
            ),
    ),
    'page_cache' =>
    array(
      'backend' => 'Cm_Cache_Backend_Redis',
      'backend_options' =>
       array(
         'server' => '127.0.0.1',
         'port' => '6379',
         'database' => '1',
         'compress_data' => '0'
       )
    )
  )
),
EOF

I also made some other attempts at solving this problem I am facing using only sed, but unfortunately I didn't save those, however, they didn't work either.

Does anybody know how I could solve this?

Edit: I am trying to perform this editing on Debian 9 "Stretch"

EDIT 2: Based on feedback I received here, I have rephrased the question here: Find and replace multiple lines of text from command line

回答1:

awk '
NR==FNR { new = new $0 ORS; next }
FNR==11 { printf "%s", new; f=1 }
!f
FNR==14 { f=0 }
' replacement.txt magento.php


回答2:

If you can use GNU sed, you could put your text in a file and replace the offending lines with the r command. If you can not, you need to fix your quoting. Or, you could use something like:

{ sed 11q old_file; cat replacement_text; sed 1,14d old_file; } > new_file