Re: Conditionals in BASH

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

 



On Wed, Apr 09, 2008 at 06:29:53PM +0000, 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.

No, they're not. However, [[ ... && ... || ... ]] are.

cf help [; help [[ and help test, and a test:

$ strace -f -e file sh -c '[ -e /tmp/a -a -e /tmp/b ]' 2>&1 | tail -2
stat64("/tmp/a", 0xbfaed638)            = -1 ENOENT (No such file or directory)
stat64("/tmp/b", 0xbfaed628)            = -1 ENOENT (No such file or directory)

$ touch /tmp/a
$ strace -f -e file sh -c '[ -e /tmp/a -o -e /tmp/b ]' 2>&1 | tail -2
stat64("/tmp/a", {st_mode=S_IFREG|0664, st_size=0, ...}) = 0
stat64("/tmp/b", 0xbfc53788)            = -1 ENOENT (No such file or directory)

I usually do [ .. ] && [ .. ] instead.

$ rm -f /tmp/a
$ strace -f -e file sh -c '[ -e /tmp/a ] && [ -e /tmp/b ]' 2>&1 | tail -2
open("/usr/lib/locale/LC/LC_TIME", O_RDONLY) = -1 ENOENT (No such file or directory)
stat64("/tmp/a", 0xbf9a12e8)            = -1 ENOENT (No such file or directory)

-- 
lfr
0/0

Attachment: pgpvgBBXy81MB.pgp
Description: PGP signature

-- 
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