1

I am attempting to handle an unexpected output from a command within a for loop. I am using a proprietary unzip utility (mfunzip), however when the utility encounters a corrupt file, it begins to echo out an endless "Fatal error in InputBit!" message.

I am attempting to capture the output and continue the loop onto the next file.

for FILE in $(ls); do
   if [ -d "$FILE" ]; then
      ...
      ...
      # without command output, it runs indefinitely
      output=$(mfunzip $currentfile $extractdir)

      # it never reaches here
      echo $output
      if [[ $output = *Fatal* ]]; then
         continue  
      fi
   fi
done

however it's not seem to be working as it blocks at the output=$(mfunzip ... ) ... I would assume it's outputting endlessly to $output. The other problem seems that the "Fatal error" may not appear immediately, but rather half-way through attempting to mfunzip the file.

Are there better ways of handling this type of output?

Sash
  • 1,134
  • 11
  • 23
  • Not related to your problem but it is much safer to use * instead of $(ls). – Abhishek Bansal Mar 03 '14 at 12:29
  • Abhishek this is not the actual script from the server, I just quickly whipped up a mockup of the for procedure in "some" folder to show where the issue is. :) – Sash Mar 03 '14 at 13:18
  • Please post code that actually reproduces your problem. Your mockup code is fine; the only problem it could have is that `mfunzip` never completes. – chepner Mar 03 '14 at 14:29

1 Answers1

0

To "handle an (unexpected or expected) output from a command", you should use : http://en.wikipedia.org/wiki/Expect , which is specially designed for this kind of things.

Olivier Dulac
  • 3,695
  • 16
  • 31