grepコマンドの文字列検索をif文にどのように入れますか?

grepコマンドの文字列検索をif文にどのように入れますか?

両方のファイルから複数の文字列を検索したいと思います。両方のファイルで文字列が見つかった場合は、その文字列で何かをします。文字列が1つのファイルでのみ見つかった場合は、別の操作を実行してください。

私のコマンドは次のとおりです。

####This is for the affirmative sentence in both files
if grep -qw "$users" "$file1" && grep -qw "$users" "$file2"; then

####This is for the affirmative sentence in only one file, and negative for the other one
if grep -qw "$users" "$file1" ! grep -qw "$users" "$file2"; then

ステートメントを拒否して確認する正しい方法はありますか? pd 私はKSHシェルを使っています。

よろしくお願いします。

ベストアンサー1

別のオプション:

grep -qw -- "$users" "$file1"; in_file1=$?
grep -qw -- "$users" "$file2"; in_file2=$?

case "${in_file1},${in_file2}" in
    0,0) echo found in both files ;;
    0,*) echo only in file1 ;;
    *,0) echo only in file2 ;;
      *) echo in neither file ;;
esac

おすすめ記事