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
Zombo
  • 1
  • 62
  • 391
  • 407
Vahe
  • 1,699
  • 3
  • 25
  • 76
  • First verify the exe is writing to stdout by piping it through windows `more` or `findstr`. Then try `windows-command | dos2unix | while read...` – Miserable Variable May 03 '13 at 01:05

2 Answers2

0

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
Zombo
  • 1
  • 62
  • 391
  • 407
  • When I do a tee after | (without ampersand) and a | (with ampersand) I see output. Unfortunately, I cannot get the output of the command to go to the loop. – Vahe May 02 '13 at 14:09
  • Does it work if you redirect stderr to stdout before piping? Like so... http://stackoverflow.com/a/6991563/759739 – Costa May 02 '13 at 14:25
  • ./inotifywait 2>&1 | while read line echo "test" done outputs "test" to the terminal but only once. I am trying to output to the loop on every new output – Vahe May 02 '13 at 14:35
  • How can I tell where inotifywait is outputting to? I am able to see output when I use the -m option in inotifywait.exe (the Windows executable being called from cygwin shell script) Is there any way to grab the output in the while read line loop of the script? – Vahe May 02 '13 at 22:23
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
Vahe
  • 1,699
  • 3
  • 25
  • 76