bash - 在 block bash 脚本中写入多行

标签 bash shell ubuntu sed

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

{
   soemthings;

   soemthings_other;
}

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

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

   soemthings_other;
}

是否可以使用 sed 来执行此操作? ?

最佳答案

创建一个名为 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;
}

关于bash - 在 block bash 脚本中写入多行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35071683/

相关文章:

linux - 打印 'find' linux 命令找到匹配项的目录

linux - 带有 crontab 的 sh 在 Centos 中不起作用

linux - 数组 - 从用户读取输入 - linux

bash - 脚本 : substitute value in script and display it

linux - cloud-init 使用元数据作为变量

django - 通过无线网络设置本地 ip 以在 Ubuntu 上创建本地服务器

bash - bash中与变量的浮点比较

bash - 将命令作为参数传递给 bash 脚本

Bash: `true` 的用法

linux - 如果由 bash 执行,如何使批处理脚本退出?