After each command, the exit code can be found in the $? variable so you .... But I haven't tested this.
stackoverflow.com