shell script replace variables in file - error with Sed's -i option for in-place updating

前端 未结 3 1051
一生所求
一生所求 2020-12-04 00:28

Here is my test.env

RABBITMQ_HOST=127.0.0.1
RABBITMQ_PASS=1234

And I want to use test.sh to replace the value in

3条回答
  •  再見小時候
    2020-12-04 01:05

    ex -sc '%!awk "\
    \$1 == \"RABBITMQ_HOST\" && \$2 = \"rabbitmq1\"\
    \$1 == \"RABBITMQ_PASS\" && \$2 = 12345\
    " FS== OFS==' -cx file
    
    1. POSIX Sed does not support the -i option. However ex can edit files in place

    2. Awk is a better tool for this, as the data is separated into records and fields

    3. In either case Sed or Awk, you can utilize a newline or ; to do everything in one invocation

    4. You have double quoted strings with no variables inside, might as well use single quotes

    5. You quoted your file name when it has no characters that need escaping

    6. You have several unquoted uses of variables, almost never a good idea

提交回复
热议问题