LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   if statement options in bash script (https://www.linuxquestions.org/questions/linux-newbie-8/if-statement-options-in-bash-script-4175472361/)

Ewais 08-06-2013 02:32 PM

if statement options in bash script
 
I have a bash script (not mine) that is generating some errors. I'm trying to fix that but there is a section that I do not understand.

Code:

snpsOALib=`OA_MODE=opt $OA_ROOT/bin/oaGetLibPath`
if [ $? != 0 ] || [ ! -d $snpsOALib ]; then
    echo "Error: Incomplete installation. OpenAccess is missing or improperly installed."
    exit 1
fi
export OA_ROOT

$OA_ROOT is a directory
I don't know what $? is .. I thought it's a variable but that is the only one written in the file(it is not defined before,neither $? nor ?).

now what does this If statement mean ?? can somebody explain ??

Firerat 08-06-2013 02:42 PM

$? is the exit code of the last command
when exit code is 0 it means no error, and none-zero code is an Error

try it with a few things

true;echo $?
false;echo $?
ffaallssee;echo $?

so what is happening which with the script is
Code:

snpsOALib=`OA_MODE=opt $OA_ROOT/bin/oaGetLibPath`
# set snpsOALib as output of command "OA_MODE=opt $OA_ROOT/bin/oaGetLibPath"
if [ $? != 0 ] || [ ! -d $snpsOALib ]; then
# if last command gave error ( not equal to 0 ) OR $snpsOALib is not a directory
    echo "Error: Incomplete installation. OpenAccess is missing or improperly installed."
# above prints error msg, below exits the script ( with exit code 1 )
    exit 1
fi
export OA_ROOT


Habitual 08-06-2013 02:47 PM

Open Audit?

err. n/m

chrism01 08-07-2013 01:14 AM

Incidentally, '!=' is the wrong operator for a numeric comparison; it should be '-ne', or use (( )) instead of [[ ]] http://tldp.org/LDP/abs/html/comparison-ops.html:


All times are GMT -5. The time now is 10:16 AM.