php env.php使用sed,awk或其他工具从bash脚本进行编辑 [英] php env.php edit from bash script using sed, awk or another tool

查看:91
本文介绍了php env.php使用sed,awk或其他工具从bash脚本进行编辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

对此:

'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编写的脚本,在尝试使用以下方法正确完成此操作之后:

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

我的猜测是我无法正常退出,但是许多Google搜索都没有找到解决方法.

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

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

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

我还尝试了其他一些尝试来解决仅使用sed时遇到的这个问题,但是很遗憾,我没有保存这些内容,但是它们也没有用.

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?

我正在尝试在Debian 9"Stretch"上执行此编辑

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

推荐答案

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

这篇关于php env.php使用sed,awk或其他工具从bash脚本进行编辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆