Re: Conditionals in BASH

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Wednesday, Apr 9th 2008 at 14:43 -0000, quoth Rick Stevens:

=>Matthew Saltzman wrote:
=>> The conditionals && and || in bash are short-circuit, so that once a
=>> term is evaluated that determines the success or failure of the entire
=>> expression, evaluation of the remaining alternatives is skipped.
=>> 
=>> In a test expression (enclosed in [ ... ]), are the conditionals -a and
=>> -o also short-circuit?  The O'Reilly BASH book seems to suggest that
=>> they are not, but the description seems to me to be ambiguous.
=>
=>To my knowledge, all conditionals are evaluated left-to-right.  As
=>soon as one comparison fails, there is no need to evaluate the remainder
=>as the true-false condition has been met and the test is aborted with
=>a "false" result.  The only time the right-most comparison (or top level
=>comparison if nested) is evaluated is if all other comparisons are
=>true.

Sheesh! We *could* actually test test to see how test tests. It turns out 
that the builtin test function (which can be written using [ -a -o ] does 
*not* short circuit. The keyword [[ && || ]] construct does.

Enjoy:


#! /bin/bash

t1()
{
    echo t1
    echo t1 1>&2
}

f1()
{
   echo ''
   echo 'f1 nada' 1>&2
}
echo
echo 'and test'
[ "$(f1)" -a "$(t1)" ] && echo ctrue
[[ "$(f1)" && "$(t1)" ]] && echo ctrue
echo
echo 'or test'
[ "$(t1)" -o "$(f1)" ] && echo ctrue
[[ "$(t1)" || "$(f1)" ]] && echo ctrue

-- 
Time flies like the wind. Fruit flies like a banana. Stranger things have  .0.
happened but none stranger than this. Does your driver's license say Organ ..0
Donor?Black holes are where God divided by zero. Listen to me! We are all- 000
individuals! What if this weren't a hypothetical question?
steveo at syslang.net

-- 
fedora-list mailing list
fedora-list@xxxxxxxxxx
To unsubscribe: https://www.redhat.com/mailman/listinfo/fedora-list
[Index of Archives]     [Older Fedora Users]     [Fedora Announce]     [Fedora Package Announce]     [EPEL Announce]     [Fedora Magazine]     [Fedora News]     [Fedora Summer Coding]     [Fedora Laptop]     [Fedora Cloud]     [Fedora Advisory Board]     [Fedora Education]     [Fedora Security]     [Fedora Scitech]     [Fedora Robotics]     [Fedora Maintainers]     [Fedora Infrastructure]     [Fedora Websites]     [Anaconda Devel]     [Fedora Devel Java]     [Fedora Legacy]     [Fedora Desktop]     [Fedora Fonts]     [ATA RAID]     [Fedora Marketing]     [Fedora Management Tools]     [Fedora Mentors]     [SSH]     [Fedora Package Review]     [Fedora R Devel]     [Fedora PHP Devel]     [Kickstart]     [Fedora Music]     [Fedora Packaging]     [Centos]     [Fedora SELinux]     [Fedora Legal]     [Fedora Kernel]     [Fedora OCaml]     [Coolkey]     [Virtualization Tools]     [ET Management Tools]     [Yum Users]     [Tux]     [Yosemite News]     [Gnome Users]     [KDE Users]     [Fedora Art]     [Fedora Docs]     [Asterisk PBX]     [Fedora Sparc]     [Fedora Universal Network Connector]     [Libvirt Users]     [Fedora ARM]

  Powered by Linux