Reputation:
So this is the gist: I have a script that execute various commands.
One of these commands, returns some logging, regarding the operation performed. I would like to intercept this logging and stop the script, if there is an error. Since the following operations in the script, are tied to the success of this operation, it makes no sense to run the whole script when you get an error at this phase.
This command is run on a remote computer via ssh; the idea would be to stop the script, as soon as the ssh command quit and the control goes back to the script
Is there a simple way to check the output of a command, which is running in a shell script? I could redirect the output to a file, and parse it, but this seems to add a lot of work extra on the script; while I need to trigger the exit only if there were errors in the ssh command.
What would you suggest to accomplish this task?
Upvotes: 0
Views: 1409
Reputation: 312630
If the commands exits with an error code when there is an error, you can do something like this:
if ! ssh somehost mycommand; then
echo "There was an error."
exit 1
fi
If it doesn't provide a useful error code, then you'll probably need to check the output using something like:
if ssh somehost somecommand 2>&1 | grep error_message; then
echo "There was an error."
exit 1
fi
These are both very common techniques, and one or both may be applicable to your needs.
Upvotes: 2