Frage

Is it possible to call a .exe file from a shell script running in Cygwin in order to pipe the result to a while read line loop?

The code I have tried is working in Linux (where inotifywait is the application I am trying in Cygwin and is installed in Linux)

I have found a .exe version of inotifywait for Windows but can not seem to get the read line loop to read from the pipe in Cygwin.

The below code is able to initialize inotifywait.exe from the shell script but never outputs any line to the Cygwin terminal.

DIR="c:/tmp" 
./inotifywait -mr -e create,modify,delete,move \
--timefmt '%m/%d/%y %H:%M:%S' --format '%T;%f;%e;%w' $DIR |
while read line
do 
  echo $line
done
War es hilfreich?

Lösung 2

I split the functionality into two files.

The first script calls inotifywait and tees the result to a file called "test"

DIR="c:/tmp" 
(inotifywait -mr --timefmt '%m/%d/%y %H:%M:%S' \
--format '%T;%f;%e;%w' $DIR) |& tee test

The second script does the while do loop on the output of tail -f test. "test" is the file name.

tail -f test |
    while read line
    do
           #form the line string with semi colons between fields
           lineString=$(echo $line | awk -F'[;]' 'echo $1;$2;$3;$4')
           #store the elements in the array
           IFS=';' read -a inotifyElements <<< "${lineString}"

           datetime=${inotifyElements[0]}             
           filename=${inotifyElements[1]}
           event=${inotifyElements[2]}
           pathname=${inotifyElements[3]}
           echo $datetime " " $filename " " $event " " $pathname 
   done

Andere Tipps

Odds are the inotifywait is outputting to stderr. If this be the case you need to pipe stderr as well

./inotifywait |& while read line
               ^
               |
               --- notice the ampersand
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top