if [[ $Something ]] – in what situations does this work?

franz.reitinger franz.reitinger at htl-wels.at
Sat Feb 18 17:23:24 UTC 2012


...
> just confuse bash scripting with other things, such as C and even
> Basic:
> if (x) {
> 	dostuff;
> 	domorestuff;
> }
>
> or
>
> if x then
> 	dostuff
> 	domorestuff
> endif
>
>

In programming languages ​​like C a return value of 0 means that an 
error has occurred.
e.g.
if ( expr ) { // expr <> 0 ==> TRUE
    ...
} else ...

A script executed within a bash shell works contrariwise;
e.g.
if [ expr ]; # you have to check the expression (expr == 0) and this 
evalutes to TRUE/FALSE

Unix based apps return a 0 for success and a value <> 0 is used for 
returning the error code.
In C (and other programming languages) a return value of 0 evalutes to 
FALSE; all other values evaluate to TRUE



>> e.g.
>> # any command ...
>> if [ $? -ne 0 ]; then
>>   # error handling
>> fi
>
> I suppose ”if [[ $? == 0 ]]” is equal to ”if [ $? -ne 0 ]”, right? 
> And
> I think that even ”if [[ $? = 0 ]]” is allowed and means the same
> thing, as far as I have seen.

Yes, there are several possibilities to write a correct command. Have a 
look to the start/stop scripts in /etc/init.d.



>> /franzR

/* 
 ************************************************************
 * Mail checked by Avira virus scanner (mail.htl-wels.at).
 ************************************************************
*/

 




More information about the ubuntu-users mailing list