Checking error codes from programs - specifically HTK Toolkit

470 Views Asked by At

I have a bash script that calls HTK components such as HCopy and HVite to compute the phone level boundaries of a piece of text. How can I check if the operation was a success? I have tried the following:

  1. ./findWordBoundary.sh <input args> |& tee run.log

The above approach does not work. Everything that is output by the HTK utilities is written to run.log, except the Error statement and condition. So using grep on run.log is a non-starter.

  1. x=./findWordBoundary.sh <input args> also does not work. It just dumps a whole load of messages, apart from the error conditions, onto x.

What I am looking for is this:
A way to log, and take appropriate action, when HTK operations result in errors. Also, I guess this can be extended to more general bash utilities, like, say, wget. How does one log and error from a bash utility and handle it, from within a script (say when wget returns a 404:Not found exception)?

Thanks,
Sriram.

1

There are 1 best solutions below

0
On BEST ANSWER

In general, Unix/Linux commands finish and 'emit' an exit code. The standard value for success is 0, and non-zero for errors.

You can see if your HT tools are emitting correctly formed exit codes like this

HTcopy ... (args ) ...
echo $?

if you setup your HTcopy so you know you should get an error AND the echo $? returns non-zero then check that a good set of command args emit 0.

If you are getting correct exit codes, you can write your code like

if HTcopy .. (args).. ; then
   printf "do the next step\n"
   #etc
else
   printf "error on HTcopy, can't continue\n"
   exit 1
fi

Another idom is to capture the exit code to a variable

HTcopy ... (args ) ...
h_rc=$?
if [[ ${h_rc} != 0 ]]] ; then
   printf "error on HTcopy, can't continue\n"
   exit 1
fi

Also, std Unix/Linux uses two separate streams for messages. STDOUT is where normal messages and success message go, while STDERR is where error messages go. You can capture the STDERR msgs by redirecting them into the STDOUT stream. Using your example above where 'grep is a non-starter', you can do

./findWordBoundary.sh <input args> 2>&1 |& tee run.log

I hope this helps.