2つのプロフィールを比較しますか?

2つのプロフィールを比較しますか?

2つの文字列は簡単に比較または類似することができますtest[[ ... ]]文字列を正規表現と比較する

例えば

wolf@linux:~$ var1=`grep 'trap2sink 127.0.0.7 w0rd' snmpd.conf`
wolf@linux:~$ 
wolf@linux:~$ if [[ $var1 = 'trap2sink 127.0.0.7 w0rd' ]]; then
> echo Good
> else
> echo Bad
> fi
Good
wolf@linux:~$ 

複数行を含むいくつかの構成セットはどうですか?

目的は、すべてのデバイスのすべての構成が標準に準拠していることを確認することです。

これがsnmpd.confの標準であると仮定します。これの名前を決めようsnmpd.conf.standard

trap2sink 127.0.0.7 w0rd
rocommunity P@55 127.0.0.1
rocommunity 4nyth1ng 127.0.0.9

これは装置Xの構成例である。私たちはそれを名前snmpd.conf

*** some random data here and there *** 
rocommunity P@55 127.0.0.1
rocommunity 4nyth1ng 127.0.0.9
trap2sink 127.0.0.7 w0rd
*** some random data here and there *** 

これら2つの構成を比較する最良の方法は何ですか?

grep複数行のデータがあるため、この場合に使用できるかどうかわからず、次にif ... elseステートメントを使用します。

if [[ ... config matches ... ]]; then
    echo Good
else
    echo Bad
fi

この種の問題を解決するための最良の方法が何であるかを教えてください。

アップデート1:ビル・ジャジャー(動作します...実際のデータでテストして結果を共有します)

wolf@linux:~$ cat snmpd.conf
*** some random data here and there *** 
rocommunity P@5s 127.0.0.1
rocommunity 4nyth1ng 127.0.0.9
trap2sink 127.0.0.7 w0rd
*** some random data here and there *** 
wolf@linux:~$ 

wolf@linux:~$ cat snmpd.conf.standard
trap2sink 127.0.0.7 w0rd
rocommunity P@55 127.0.0.1
rocommunity 4nyth1ng 127.0.0.9
wolf@linux:~$ 

wolf@linux:~$ ptn='^(rocommunity|trap2sinc) ';
wolf@linux:~$ diff <(grep -E "$ptn" snmpd.conf | sort) <(grep -E "$ptn" snmpd.conf.standard | sort);
2c2
< rocommunity P@5s 127.0.0.1
---
> rocommunity P@55 127.0.0.1
wolf@linux:~$ 

ベストアンサー1

ptn='^(rocommunity|trap2sinc) ';
diff <(grep -E "$ptn" file1| sort) <(grep -E "$ptn" file2| sort);

何も返さない場合、その行は両方のファイルに共通です。

おすすめ記事