Is there any way to check if there is an error in executing a command?
Example :
test1=`sed -i "/:@/c connection.url=jdbc:oracle:thin:@$ip:1521:$dataBase" $search`
valid $test1
function valid () {
if $test -eq 1; then
echo "OK"
else echo "ERROR"
fi
}
I already tried do that but it seems it isn't working. I don't how do that.
The return value is stored in
$?
. 0 indicates success, others indicates error.Like any other textual value, you can store it in a variable for future comparison:
For possible comparison operators, see
man test
.If you only need to know if the command succeeded or failed, don't bother testing
$?
, just test the command directly. E.g.:And assigning the output to a variable doesn't change the return value (well, unless it behaves differently when stdout isn't a terminal of course).
http://mywiki.wooledge.org/BashGuide/TestsAndConditionals explains
if
in more detail.$? should contain the exit status of the previous command, which should be zero for no error.
So, something like;
for most cases, it's easier to use the && construct to chain commands that need to depend on each other. So
cd /nonexistant && echo success!
would not echo success because the command breaks before &&. The corollary of this is ||, wherecd /nonexistant || echo fail
would echo fail because cd failed. (this becomes useful if you use something like ||exit, which will end the script if the previous command failed.)It should be noted that
if...then...fi
and&&
/||
type of approach deals with exit status returned by command we want to test( 0 on success ); however, some commands don't return a non-zero exit status if command failed or couldn't deal with input. This means that the usualif
and&&
/||
approaches won't work for those particular commands.For instance, on Linux GNU
file
still exits with 0 if it received a non-existing file as argument andfind
couldn't locate the file user specified.In such cases, one potential way we could handle the situation is by reading
stderr
/stdin
messages, e.g. those that returned byfile
command, or parse output of the command like infind
. For that purposes,case
statement could be used.( This is a repost of my own answer on related question at unix.stackexchange.com )
As mentioned in many other answers, a simple test of
$?
will do, like thisIf you want to test if the command failed, you can use a shorter version in
bash
(but perhaps overkill) as follows:This works by using the
(( ))
arithmetic mode, so if the command returnedsuccess
, i.e.$? = 0
then the test evaluates to((0))
which tests asfalse
, otherwise it will returntrue
.To test for success, you could use:
but it's already not much shorter than the first example.
For easier debugging, I make commands to only output errors with:
After that $? is 0 if success, otherwise failure.
You can also configure bash to exit if a command fails, useful to avoid checks after every command. Place this at beginning of your script:
prints
passed
if last command passedprints
failed
if last command failedif [ $? == 0 ]; then echo passed;else echo failed; (exit 1); fi;