2016-08-26 30 views
0

我有两个文件main.css和main_custom.css。我想在关键字的帮助下在main.css中找到合适的位置,并在标识的位置插入main_custom.css的内容。我正在尝试使用shell脚本来达到这个目的。每个文件的代码如下所示。Shellscript不读取结束大括号

的main.css

[SOME_STUFF] 
/* 
========================================================================== 
    Author's custom styles 
========================================================================== 
*/ 
[SOME_MORE_STUFF] 

main_custom.css

body { 
    padding-top: 50px; 
    padding-bottom: 20px; 
} 

script.sh

# Merge custom CSS into master CSS file 
LINE_NO=0 
while read LINE 
do 
    ((LINE_NO++)) 
    if [[ $LINE == *"Author"* ]] 
    then 
     read LINE 
     ((LINE_NO++)) 
     if [[ $LINE == *"=="* ]] 
     then 
      read LINE 
      ((LINE_NO++)) 
      break 
     fi 
    fi 
done <"main.css" 

# Merge custom CSS into master CSS file 
while IFS= read -r CUSTOM_LINE 
do 
    sed -i -e "$LINE_NO i $CUSTOM_LINE" "main.css" 
    ((LINE_NO++)) 
done <"main_custom.css" 

生成的文件看起来是这样的 -

的main.css

[SOME_STUFF] 
/* 
========================================================================== 
    Author's custom styles 
    ========================================================================== 
*/ 
body { 
padding-top: 50px; 
padding-bottom: 20px; 
[SOME_MORE_STUFF]  

现在,当我执行脚本,脚本会将前三行main_custom的.css文件,但不是用大括号括起来的文件。另外,我也在失去格式化。我究竟做错了什么?

+0

请执行文件都有一个结尾的新行? – Eris

+0

@Eris - main_custom.css没有尾随换行符。但是,如果我补充一点,'sed'在'a','c'或'i''后面抱怨错误信息'sed:-e expression#1,char 6:expected \'。 –

回答

1

这里是一个sed oneliner替代脚本 - 右括号必须是一个换行符

sed '/Author\x27s custom styles/{n; rmain_custom.css 
}' 

输出采样输入

[SOME_STUFF] 
/* 
========================================================================== 
    Author's custom styles 
========================================================================== 
body { 
    padding-top: 50px; 
    padding-bottom: 20px; 
} 
*/ 
[SOME_MORE_STUFF]