0

シェルスクリプトを使用してecho > データを送信し$outputfileていますが、以前のデータを含む古いファイルが常にあります$oldouptdata。私がする必要があるのは次のとおりです。

echo "datahereto" > $ouptputfile 
read  $oldouptdata
diff  $ouptputfile  $olouptdata
if there is a difference   then execute function  (myfunction)    then   replace $oldoutputdata  data with $outputfile data 

これは私のシナリオです。それで何か助けはありますか?

4

2 に答える 2

1

この小さなbashスクリプトでうまくいくはずです

#!/bin/bash

echo "yourstuff" > outputfile

diff outputfile oldfile

if [ $? == 1 ]; then

    # they're different!

    # call your function
    yourfunction

    # subst old with new one
    cp -f outputfile oldfile

else

    # they're the same

fi;
于 2013-01-07T13:35:05.883 に答える
0

それ以上の情報がなければ、前のファイルを置き換えることができると思います。oldfileがと同じ場合newfile、置換はnoopになります。異なる場合は、目的の状態にもなります。

何らかの理由で、oldfile実際にと異なる場合にのみ交換する必要がある場合はnewfile、次のようなものを使用してください

if ! cmp -s oldfile newfile; then
  mv newfile oldfile
fi
于 2013-01-07T13:29:20.690 に答える