php env.php使用sed,awk或其他工具从bash脚本编辑

时间:2017-05-08 03:48:18

标签: bash awk sed replace

使用bash脚本,我正在尝试编辑Magento 2 env.php配置文件。 bash脚本首先执行一些其他操作。 它需要在第11-14行的配置文件中更改此部分:

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

进入这个:

'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'
       )
    )
  )
),

我试图用sed和awk的各种方法实现并已经花费了多个小时,但我无法弄清楚我做错了什么。我以前没有sed,awk或shell脚本的经验。在我无法实现我真正想要的东西之后,你可以在每个GUI文本编辑器中执行“搜索和替换”,我最终通过引用行号并尝试添加了sed来删除sed的旧内容。使用awk的正确内容。我已经尝试在每个句子的末尾使用反斜杠。 这是我用sed和awk编写的脚本,在第一次尝试使用以下方法正确使用:

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

然而,我得到的结果是:

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

我的猜测是我没有正常退出,但许多谷歌搜索都没有找到解决方案。

这是使用cat和sed的另一次尝试,它也不起作用并导致空文件:

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

我还做了一些其他尝试来解决这个问题,我只面对使用sed,但不幸的是我没有保存那些,但是,它们也没有用。

有人知道如何解决这个问题吗?

编辑:我正在尝试在Debian 9“Stretch”上执行此编辑

编辑2:根据我在此收到的反馈,我在此重述了这个问题:Find and replace multiple lines of text from command line

0 个答案:

没有答案