【问题标题】:Write multiple line in block bash script在块 bash 脚本中写入多行
【发布时间】:2016-05-06 10:19:51
【问题描述】:

我想使用 bash 在文件中编写一个脚本,这个脚本是自动生成的,假设我有一个名为 some.sh 的文件,内容如下:

{
   soemthings;

   soemthings_other;
}

我想将脚本添加到 some.sh 变成这样

{
   soemthings;

   # the script 
   {
      hello there
      im the new script
      and multilines 
   }

   soemthings_other;
}

是否可以使用sed 做到这一点?

【问题讨论】:

  • 添加文本块的标准是什么?在第一个空白行?在特定的行号?在某些特定字符串之前/之后/之间?还有什么?要添加的文本块是否存储在文件中?一个变量?还有什么?有没有不能包含的特定字符?基本上 - 提供一些关于您的要求的信息。

标签: bash shell ubuntu sed


【解决方案1】:

像这样创建一个名为 script.tmp 的文件:

   # the script
   {
      hello there
      im the new script
      and multilines
   }

然后使用此sed 将其插入some.sh

sed -i.bak '/soemthings;/r script.tmp' some.sh

cat some.sh
{
   soemthings;
   # the script
   {
      hello there
      im the new script
      and multilines
   }

   soemthings_other;
}

【讨论】:

    猜你喜欢
    • 2019-12-30
    • 2023-03-23
    • 2012-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多