What does this ( if [$? -ne 0] ) mean in unix ? Linux

Posted by Jiltin     6 February, 2009    3,435 views   

$? is the return code form a previous command or function
in unix, 0 is the standard return code for a successful completion

if [$? -ne 0]
The whole line means

‘if the previous return code was not equal to 0 ‘ ….

It will then be followed by

then
statement # do something
(optional) else
(optional) statement # do something else
fi # closure of the ‘if’ conditional

For example

scp filename login@hostname:filename
if [ $? -ne 0 ]
then  
        echo "Error Occured While SCP"
    else
        echo "———————————————————"
        echo "successful transmission "
        echo "———————————————————"
fi

Post to Twitter  Post to Delicious  Post to Digg    Post to StumbleUpon

Categories : Scripts Unix Tags : , ,

Comments

No comments yet.


Leave a comment

(required)

(required)