if [[ $Something ]] – in what situations does this work?
Steven Davies-Morris
sdavmor at systemstheory.net
Sat Feb 18 19:44:49 UTC 2012
On 02/18/2012 09:33 AM, Robert P. J. Day wrote:
> On Sat, 18 Feb 2012, franz.reitinger wrote:
>
>> ...
>>> 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
>
> let me throw in a bit of info that might clear up a lot of
> confusion. one of the forms of the "if" construct in shell
> programming is:
>
> if<some command> ; then
>
> where you're testing the exit code ($?) of some command you're
> running. linux commands normally return 0 if they work, and the "if"
> construct treats that value as "true". (the above is normally called
> a "command test" in shell programming.)
>
> there are four general tests in shell programming:
>
> 1) string tests
> 2) numeric tests
> 3) file tests
> 4) command tests
>
> but (and this is important), they are ***all*** just variations of
> command tests. ***all*** of them. in every single case, you should
> interpret the "if" construct as operating on the result being returned
> as if a command had just been run.
>
> this confuses people who look at something like:
>
> if [ $VAR = "rday' ] ; then
>
> and think it's a string comparison. well, ok, it is, but more
> fundamentally, it's the *command*
>
> [ $VAR = "rday" ]
>
> being executed, then returning the appropriate value of $?
>
> that '[' thing? it's not just a bracket. it's a command:
>
> $ type [
> [ is a shell builtin
> $
>
> it is (for the most part) precisely equivalent to forms you might
> have seen in older shell programs:
>
> if test $VAR = "rday" ; then
>
> the '[' object is simply another form of the "test" command. is all
> this making sense?
>
> one last time -- every usage of "if" in a shell script can be
> thought of as a command test.
>
> rday
>
>
> ========================================================================
> Robert P. J. Day Ottawa, Ontario, CANADA
> http://crashcourse.ca
>
> Twitter: http://twitter.com/rpjday
> LinkedIn: http://ca.linkedin.com/in/rpjday
> ========================================================================
A nice and very clear synopsis, Robert. Thanks.
--
SDM a 21st century schizoid man in SoCal
Systems Theory website www.systemstheory.net
"overfulnoisecascade" coming soon
More information about the ubuntu-users
mailing list