2012-11-16 42 views
1

的內容,我想一個數組的內容比較文件的內容比較慶典陣列的內容。我想最好的解決辦法是:最優雅的技術與文件

b=(some data) 
a=$(<file) 
if [ $a -ne ${b[@]} ] 
then 
    echo "variables are different" 
fi 

我說得對嗎?

+0

您需要定義數組的預期表現是什麼。你是否打算將這些物品分隔空間,換行分開,引用或者是什麼? – ams

+0

@ams:字的分裂將重新排列東西 –

+0

注意'-ne'是Bourne shell的*算術*,沒有文字,比較,是你在找什麼?文件是多行的嗎? – cdarke

回答

1

試試這個:

$ cat file 
a 
b 
c 
$ echo -n "arrays are " 
$ x1=(a b c) 
$ mapfile -t x2 < file 
$ [[ ${x1[@]} == ${x2[@]} ]] && echo "identical" || echo >&2 "different" 
+0

你可以用'映射文件-t X2

+0

我沒有mapfile,我的Archlinux不知道如何下載它 –

+1

這是一個'bash'內建! 'help mapfile' –

-1

通過使用bash的進程替換:

b=(some data) 
if ! diff <(echo ${b[*]}) file; then 
    echo "different" 
fi 
+0

-1:http://pastie.org/5390999 –