Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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.

share|improve this question
4  
Prefer $(foo) over backticks `foo`, because you can nest it, and it's easier to distinguish from apostrophes. –  user unknown Mar 26 '11 at 18:19

4 Answers 4

up vote 40 down vote accepted

The return value is stored in $?. 0 indicates success, others indicates error.

some_command
if [ $? -eq 0 ]; then
    echo OK
else
    echo FAIL
fi

Like any other textual value, you can store it in a variable for future comparison:

some_command
retval=$?
do_something $retval
if [ $retval -ne 0 ]; then
    echo "Return code was not zero but $retval"
fi

For possible comparison operators, see man test.

share|improve this answer
    
That's nice ...can i hold the output error ??!! , because in this case i have 2 error : command error "text" ex, file not found and my error "text" Which is in this case failed for example –  moata_u Mar 7 '11 at 12:28
    
@moata_u: you can store the value in a variable as shown in my answer. –  Lekensteyn Mar 7 '11 at 13:14
    
@moata_u: You need to put a ; before the else and fi: `if ...; then ...; else ...; fi –  Lekensteyn Mar 7 '11 at 14:36
    
please follow askubuntu.com/questions/29412/error-checking –  moata_u Mar 7 '11 at 16:16
    
man test only shows information about the external test command, not the shell's builtin test command; see the shell's manual for that. In bash you can also get info on builtins and keywords with the help builtin; help test. –  geirha Mar 8 '11 at 23:58

If you only need to know if the command succeeded or failed, don't bother testing $?, just test the command directly. E.g.:

if some_command; then
    printf 'some_command succeeded\n'
else
    printf 'some_command failed\n'
fi

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).

if output=$(some_command); then
    printf 'some_command succeded, the output was «%s»\n' "$output"
fi

http://mywiki.wooledge.org/BashGuide/TestsAndConditionals explains if in more detail.

share|improve this answer

$? should contain the exit status of the previous command, which should be zero for no error.

So, something like;

cd /nonexistant
if [ $? -ne 0 ]
then
    echo failed
else
    echo success!
fi

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 ||, where cd /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.)

share|improve this answer
    
That's nice ...can i hold the output error ??!! , because in this case i have 2 error : command error "text" ex, file not found and my error "text" Which is in this case failed for example –  moata_u Mar 7 '11 at 12:24
    
@moata_u, see mywiki.wooledge.org/BashFAQ/002 –  geirha Mar 8 '11 at 23:55
command && echo OK || echo Failed
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.