0

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
4

2 回答 2

0

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
于 2013-05-03T01:01:30.400 回答
0

赔率inotifywait是输出到标准错误。如果是这种情况,您还需要管道 stderr

./inotifywait |& while 读取行
               ^
               |
               --- 注意与号
于 2013-05-02T01:09:48.143 回答