Display Output of Shell Script as It Runs (Before Completion)

I have a shell script that runs a lengthy conversion process on an input file. As the script runs, the shell progressively logs more info, like this:

Part 01/03 - Converting...
Part 01/03 - Conversion finished!
Part 02/03 - Converting...
Part 02/03 - Conversion finished!
Part 03/03 - Converting...
Part 03/03 - Conversion finished!

Once the script completes, it outputs something like:

File X - Conversion complete (03 parts converted)!

I am able to get the full shell output of the script once it completes. However, I'd like to display progress notifications via KBM as the script is running. For example, pop a new notification whenever a new line in the output contains Conversion finished!.

Is this possible? Happy to elaborate if this is unclear. Basically, I want to track the shell output as the script is running, instead of only after it completes.

If you can modify your shell script so that it pipes its output into a file, (while it also displays it to the screen) then the solution is easy. A KM Macro could (in a loop) just examine the contents of the file and generate a message. The shell command "tee" is designed just for this purpose.

If you cannot modify your script, then you could use OCR to read the Terminal's window. That would be roughly the same level of difficulty.

I can think of another method, but you will have to explain exactly what is happening behind each of those messages.

Which method interests you?

A couple of ways immediately spring to mind:

Instead (or as well as) echoing to a log:

  • echo each "Conversion finished!" line to a new file in a set folder. Have a macro, triggered by additions to that folder, that throws a dialog/notification using the file's text and deletes the file
  • have a macro that throws a dialog/notification using text sent as a parameter, include in your shell script a Command Line Tool call that calls that macro, passing the text as the parameter

Both these assume you can edit the shell script. If not, just monitor the log for changes, evaluate the last line(s) (the tail utility will be useful here), and act accordingly. You can fire off a "monitor" macro before your shell script and cancel it after the script completes so it isn't needlessly running 24/7.