2016-01-28 83 views
1

我想使用bash寫一個文件中的腳本,自動生成該腳本,可以說,我有一個名爲some.sh文件,內容如下:寫塊bash腳本多行

{ 
    soemthings; 

    soemthings_other; 
} 

我想添加腳本some.sh變成這個樣子

{ 
    soemthings; 

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

    soemthings_other; 
} 

是有可能做到這一點使用sed

+0

什麼是對其中添加文本塊的標準是什麼?在第一個空白行?在特定的行號?在某些特定字符串之前/之後/之間?還有別的嗎?要添加的文本塊是否存儲在文件中?一個變量?還有別的嗎?有沒有特定的字符不能包含?基本上 - 提供一些關於您的要求的信息。 –

回答

3

創建一個名爲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; 
}