Kyle Kyle - 3 months ago 16
Bash Question

BASH - Function returns wrong value

It has been awhile since I scripted. I was never good to begin with..

I am working on a script that would test an oracle DB connection.

testDB() {
[[ $(tnsping $tnsName | grep -c WRONG)==1 ]] && return 1
return 0
}

testDB
status="${?}"
echo "Status value= " $status


I am expecting a 0 value from my echo but somehow my status is showing 1 in my echo.

What am I doing wrong here?

EDIT2:

My original code was:

testDB() {
[[ $(tnsping $tnsName | grep -c OK) -eq 1 ]] && return 1
return 0
}


my $tnsName contains a correct value and the grep count would be 1, yet my functions returns 0.

agc agc
Answer

Variables not needed, nor grep -c. Just use grep -q (no output), to return a true/false flag:

testDB() {
    tnsping $tnsName | grep -q OK
    }

testDB
echo "Status value= " $?
Comments