Shell:単純なifステートメントが機能していないようです。

Shell:単純なifステートメントが機能していないようです。

私は次のようなシェルスクリプトを作成しました。

while inotifywait -e modify $ENV_LOCATION/*.env
do
  md5sum $ENV_LOCATION/*.env > ./checksums_optwo.md5
  if [ -n "$(cmp ./checksums_opone.md5 ./checksums_optwo.md5)" ]
  then
    gdialog --msgbox "The files are different"
    md5sum $ENV_LOCATION/*.env > ./checksums_opone.md5
  else
    gdialog --msgbox "The files match"
  fi
done

gdialogしかし、なぜプロンプトがないのかわかりません。どんなアイデアがありますか?

ベストアンサー1

gdialogステートメントの両方の分岐があるため、終了状態がゼロの状態で終了する限り、どちらか一方が実行されますifinotifywait監視中のファイルのいずれかが削除された場合(そして削除イベントを監視しない場合)、コマンドはinotifywaitゼロ以外の終了状態で終了します。

ファイルの削除と変更を監視するには、次のようにします。

inotifywait -e modify -e delete_self "$ENV_LOCATION"/*.env

両方のファイルを比較し、cmp結果に反応します。

if cmp -s file1 file2; then
    echo 'files are the same'
else
    echo 'files are different'
fi

おすすめ記事