48

ファイルが /bin/sh で 30 分以上経過しているかどうかを判断するスクリプトを作成するにはどうすればよいですか?

stat残念ながら、コマンドはシステムに存在しません。古い Unix システムです。http://en.wikipedia.org/wiki/Interactive_Unix

残念ながら、Perl はシステムにインストールされておらず、顧客は Perl をインストールすることを望んでいません。

4

12 に答える 12

24

sh スクリプトを作成している場合、最も便利な方法は、test前述の stat トリックを使用することです。

if [ `stat --format=%Y $file` -le $(( `date +%s` - 1800 )) ]; then 
    do stuff with your 30-minutes-old $file
fi

[へのシンボリックリンク (または同等のもの) であることに注意してくださいtest。を参照してください。ただし、およびも bash 組み込みであるため、動作がわずかに異なる可能性がman testあることに注意してください。( bash 複合コマンドにも注意してください)。test[[[

于 2010-01-05T11:24:56.203 に答える
12

わかりました、統計はなく、不自由な発見です。代替案は次のとおりです。

GNU coreutilsをコンパイルして、適切な検索 (およびその他の便利なコマンド) を取得します。すでに gfind として持っているかもしれません。

dateうまくいけば、ファイルの変更時間を取得するために使用できます-rか?

(`date +%s` - `date -r $file +%s`) > (30*60)

または、比較を使用してどちらの-ntファイルが新しいかを選択します。問題は、mod 時刻が 30 分前のファイルを作成することです。 touch通常はそれを行うことができますが、利用可能なものに関してはすべての賭けがオフになっています.

touch -d '30 minutes ago' 30_minutes_ago
if [ your_file -ot 30_minutes_ago ]; then
    ...do stuff...
fi

そして最後に、シェル ユーティリティのバージョンを誰が知っているかで苦労するのではなく、Perl が利用可能かどうかを確認します。

use File::stat;
print "Yes" if (time - stat("yourfile")->mtime) > 60*30;
于 2010-01-05T13:55:55.090 に答える
5

@slebetmanの回答に基づいて、バックティックが嫌いな私のような人のために:

echo $(( $(date +%s) - $(stat -L --format %Y $filename) > (30*60) ))
于 2015-02-24T22:20:42.223 に答える
2
#!/usr/bin/ksh
## this script creates a new timer file every minute and renames all the previously created timer files and then executes whatever script you need which can now use the timer files to compare against with a find.  The script is designed to always be running on the server.  The first time the script is executed it will remove the timer files and it will take an hour to rebuild them (assuming you want 60 minutes of timer files)

set -x

# if the server is rebooted for any reason or this scripts stops we must rebuild the timer files from scratch
find /yourpath/timer -type f -exec rm {} \;

while [ 1 ]
do
COUNTER=60
COUNTER2=60
cd /yourpath/timer
while [ COUNTER -gt 1 ]
do
  COUNTER2=`expr $COUNTER - 1`
  echo COUNTER=$COUNTER
  echo COUNTER2=$COUNTER2
  if [  -f timer-minutes-$COUNTER2 ]
    then
       mv timer-minutes-$COUNTER2 timer-minutes-$COUNTER
       COUNTER=`expr $COUNTER - 1`
  else
     touch timer-minutes-$COUNTER2
  fi
done

touch timer-minutes-1
sleep 60

#this will check to see if the files have been fully updated after a server restart
COUNT=`find . ! -newer timer-minutes-30 -type f | wc -l | awk '{print $1}'`
if [ $COUNT -eq 1  ]
   then
     # execute whatever scripts at this point
fi

done
于 2011-06-07T19:42:34.843 に答える
2

30 分以上前に変更された、または30分以上前に作成された30 分より古いとはどういう意味ですか? これまでの回答はその解釈に対して正しいので、うまくいけば前者です。後者の場合、UNIX ファイル システムはファイルの作成時間を追跡しないため、問題が発生します。( file 属性は、inodeの内容が最後に変更されたとき、つまり何かが起こったときなどを記録します)。ctimechmodchown

ファイルが 30 分以上前に作成されたかどうかを本当に知る必要がある場合は、ファイル システムの関連部分を繰り返しスキャンするか、 Linux のinotifyfindのようなプラットフォームに依存するものを使用する必要があります。

于 2010-01-05T09:57:20.297 に答える
2

検索コマンドを使用できます。
たとえば、現在のディレクトリで 30 分以上経過したファイルを検索するには、次のようにします。

find . -type f -mmin +30 

findコマンドについては、こちらを参照してください。

于 2017-10-13T07:27:11.843 に答える