私は走っているfs_usage
私のファイルシステムのオブジェクトへのアクセスを検出します。
sudo fs_usage -w | grep -E 'object'
touch
5秒以内に上記のコマンドの新しい出力がない限り、このオブジェクトに対して5秒ごとにコマンドを実行したいと思います。
答え1
sudo fs_usage -w | while true; do
if read -rt5 && [[ $REPLY =~ objectpattern ]]; then
# Some output happened in the last 5 seconds that matches object pattern
:
else
touch objectfile
fi
done
もちろん使用することは、read -t
いくつかの出力が可能であることを意味します。いいえmatch objectpattern
;これが発生すると、ファイルがタッチされます。これを避けたいなら、もう少し洗練されなければなりません。
timeout=5
sudo fs_usage -w | while true; do
(( mark = SECONDS + timeout ))
if !read -rt$timeout; then
touch objectfile
timeout=5
elif ![[ $REPLY =~ objectpattern ]]; then
# Some output happened within timeout seconds that does _not_ match.
# Reduce timeout by the elapsed time.
(( timeout = mark - SECONDS ))
if (( timeout < 1 )); then
touch objectfile
timeout=5
fi
else
timeout=5
fi
done
答え2
私が正しく理解した場合は、おそらく次のことをしたいと思います。
sh -c '{ fsusage #your command runs (indefintely?)
kill -PIPE "$$" #but when it completes, so does this shell
} >&3 & #backgrounded and all stdout writes to pipe
while sleep 5 #meanwhile, every 5 seconds a loop prints
do echo #a blank line w/ echo
done' 3>&1 | #also to a pipe, read by an unbuffered (GNU) sed
sed -u '
### if first input line, insert shell init to stdout
### for seds [aic] commands continue newlines w/ \escapes
### and otherwise \escape only all other backslashes
1i\
convenience_func(){ : this is a function \\\
declared in target \\\
shell and can be \\\
called from sed.; }
### if line matches object change it to command
/object/c\
# this is an actual command sent to a shell for each match
### this is just a comment - note the \escaped newlines above
### delete all other nonblank lines; change all blanks
/./d;c\
# this is a command sent to a shell every ~5 seconds
' | sh -s -- This is the target shell and these are its \
positional parameters. These can be referred \
to in sed\'s output like '"$1"' or '"$@"' as \
an array. They can even be passed along to \
'convenience_func()' as arguments.
上記のうち約90%がコメントです。基本的に結論は...
sh -c '(fsusage;kill "$$") >&3 &
while sleep 5; do echo; done
' 3>&1|
sed -nue '/pattern/c\' -e 'echo match
/./!c\' -e 'touch -- "$1"
' | sh -s -- filename