2012-08-09 23 views
2

我正在編寫一個腳本以使用GNU版本的diff命令找出文件之間的差異。在這裏,我需要忽略html註釋<! - 以及任何匹配的模式(通過文件提供的輸入)。當選項作爲變量傳遞時diff -I(大寫字母'i')不工作

文件wxy/a

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns:p="http://www.springframework.org/schema/p" 
     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"> 
some text here 
      <property name="loginUrl" value="http://localhost:15040/ab/ssoLogin"/> 
    <!--property name="cUrl" value="http://localhost:15040/ab/ssoLogin" /--> 
</beans> 

文件xyz/a

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns:p="http://www.springframework.org/schema/p" 
     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"> 
some text there 
      <property name="loginUrl" value="http://localhost:15045/ab/ssoLogin"/> 
    <!--property name="cUrl" value="http://localhost:15045/ab/ssoLogin" /--> 
</beans> 

模式輸入文件:input.conf

[a] 
http://.*[:0-9]*/ab/ssoLogin 
[some other file] 
.... 
.... 

我的腳本將讀取的文件名input.conf中的[一]並放到一個臨時文件lines_to_ignore,現在我讀取fil Ëlines_to_ignore和圖案追加到可變像下面

compare_file.sh

diff_ignore_options="-I \"\!--\"" # Ignore option for <!-- Comments 

for iline in `cat lines_to_ignore`; do 
    diff_ignore_options=${diff_ignore_options}" -I \"$iline\"" 
    echo "-----------------------------------------------------------" 
    diff -I "\!--" -I "$iline" wxy/a xyz/a 
    echo "-----------------------------------------------------------" 
done 

diff $diff_ignore_options wxy/a xyz/a 

現在的輸出:

----------------------------------------------------------- 
19c19 
< some text here 
--- 
> some text there 
----------------------------------------------------------- 

19,21c19,21 
< some text here 
<   <property name="loginUrl" value="http://localhost:15040/ab/ssoLogin"/> 
<  <!--property name="cUrl" value="http://localhost:15040/ab/ssoLogin" /--> 
--- 
> some text there 
>   <property name="loginUrl" value="http://localhost:15045/ab/ssoLogin"/> 
>  <!--property name="cUrl" value="http://localhost:15045/ab/ssoLogin" /--> 

爲什麼不工作在diff命令變量替換?

diff $diff_ignore_options wxy/a xyz/a 

我想這樣做的方式是可變的,因爲我可能必須匹配一些文件中的多個模式。

回答

3

問題是外殼用於歷史擴展的!字符。此外,您在$diff_ignore_options變量中包含了轉義雙引號字符;由於您要忽略的模式不包含任何"字符,因此您不需要這些字符。

這應該工作(注意,使用單引號,以避免治療!元字符):

diff_ignore_options='-I !--' 
diff $diff_ignore_options this_file that_file 

然後你就可以添加更多的模式是這樣的:

diff_ignore_options="$diff_ignore_options -I foobar" 
+0

非常感謝。有效。 – rtv 2012-08-09 20:04:21

+1

@rtv:我看到你是Stack Overflow的新手。歡迎! 「非常感謝」通常用upvote表示(單擊答案旁邊的向上箭頭)。 「它的工作」通常通過接受答案來表示(點擊複選標記)。 (當然,這兩個都不是強制性的。)請參閱http://stackoverflow.com/faq#howtoask當然,歡迎您。 – 2012-08-09 20:07:43

+0

我嘗試點擊upvote,它說我需要最少15個代表。 – rtv 2012-08-09 20:10:12

相關問題