diff --git a/onfilechange.sh b/onfilechange.sh index 1d9f36c10945115e9712e76665ac8708346bde93..8ead4dd4f0e76360ae22419e93329a9e4a945eac 100755 --- a/onfilechange.sh +++ b/onfilechange.sh @@ -10,14 +10,15 @@ # It loops permanently; you need to stop it by Ctrl + C and/ or can # use it as systemd watcher daemon. # -# It uses stats for wide compatibility but prefers inotifywatch to -# check a file change. +# It uses stat for wide compatibility but can enable inotifywatch to +# check a file change by an event. # # licence: GNU GPL 3.0 # source: https://git-repo.iml.unibe.ch/iml-open-source/onfilechange # # ---------------------------------------------------------------------- -# 2019-10-14 v1.0 <axel.hahn@iml.unibe.ch> first basic version +# 2019-10-14 v1.0 <axel.hahn@iml.unibe.ch> first basic version +# 2019-10-21 v1.03 <axel.hahn@iml.unibe.ch> use stat as default # ====================================================================== # ---------------------------------------------------------------------- @@ -31,7 +32,7 @@ sWatchFile= sMode= # ---- below are some internal variables -sVersion=1.02 +sVersion=1.03 # ---------------------------------------------------------------------- @@ -55,11 +56,11 @@ PRAMETERS: -i force inotifywait command -s - force stats command + force stat command (default mode) -v verbose mode; enable showing debug output -w [integer] - for stats mode: wait time in seconds betweeen each test or on missing file; default: 5 sec + for stat mode: wait time in seconds betweeen each test or on missing file; default: 5 sec EXAMPLES: `basename $0` -f /home/me/touchfile.txt -c "ls -l" @@ -68,8 +69,8 @@ EXAMPLES: `basename $0` -f "/home/me/touchfile.txt home/me/touchfile2.txt" -c "ls -l" watch touchfile.txt and touchfile2.txt - `basename $0` -f /home/me/touchfile.txt -s -w 10 -c "ls -l" - watch touchfile.txt every 10 sec with stats and make a file listing on change + `basename $0` -f /home/me/touchfile.txt -s -w 10 -c "echo hello" + watch touchfile.txt every 10 sec with stat and show "hello" on a change ENDOFHELP @@ -94,7 +95,7 @@ function listFiles(){ ls -ld ${sWatchFile} 2>&1 } -# for stats: helper to get current file status +# for stat: helper to get current file status # # global (string) $sWatchFile function getFilestatus(){ @@ -104,7 +105,7 @@ function getFilestatus(){ done } -# for stats: inititalize file change detection +# for stat: inititalize file change detection # # global (string) $TmpFile last/ initial file status # global (string) $sTmpFile2 current file status @@ -114,7 +115,7 @@ function initFilestatus(){ cp -p ${sTmpFile} ${sTmpFile2} } -# for stats: compare file status and execute command on change +# for stat: compare file status and execute command on change # # global (string) $TmpFile last/ initial file status # global (string) $sTmpFile2 current file status @@ -163,19 +164,19 @@ ______________________________________________________________________________ ENDOFHEAD -which inotifywait >/dev/null 2>&1 +which stat >/dev/null 2>&1 if [ $? -ne 0 ]; then - echo ERROR: the command inotify was not found on your system, - which stat >/dev/null 2>&1 + echo ERROR: the command stat was not found on your system. + which inotifywait >/dev/null 2>&1 if [ $? -ne 0 ]; then - echo ERROR: the command stat was not found on your system, + echo ERROR: the command inotifywait was not found on your system. exit 2 fi - echo "INFO: enabling compatibility mode with stats command" - sMode=stats -else - echo "INFO: inotifywait command detected" + echo "INFO: enabling inotifywait command" sMode=inotifywait +else + echo "INFO: stat command detected" + sMode=stat fi if [ $# -eq 0 ]; then @@ -207,8 +208,8 @@ do wd "forcing mode with inotifywait command" ;; s) - sMode=stats - wd "forcing mode with stats command" + sMode=stat + wd "forcing mode with stat command" ;; w) typeset -i iSleep=$OPTARG @@ -222,10 +223,12 @@ do done cat <<ENDOFINFO + +--- summary checking file [${sWatchFile}] with command [${sMode}] -with a sleep time of [${iSleep}] seconds -and on change start command [${sCommand}] +with a sleep time of [${iSleep}] seconds +and on change I start the command [${sCommand}] ............................................................................... @@ -271,7 +274,7 @@ case $sMode in inotifywait -e attrib -e modify ${sWatchFile} && execCommand else echo "ERROR: inotifywait only can notify if all watched files exist." - echo "Use parameter -s to use stats for file detection, This mode also allows that a file is deleted." + echo "Use parameter -s to use stat for file detection, This mode also allows that a file is deleted." wd sleep ${iSleep} sleep ${iSleep} exit 2 @@ -279,7 +282,7 @@ case $sMode in done ;; - "stats") + "stat") wd "--- initial read of watched files" initFilestatus echo waiting for file changes ...